Java-Programs-asked-in-Selenium-Interview

1. How to Print List of Even Numbers in Java?

 package JavaPrograms_InterviewQuestions;

 import java.util.Scanner;

 public class PrintEvenNumbers {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println(“Enter Limit:”);

int value = sc.nextInt();

System.out.println(“Print Even numbers between 1 and ” + value);

for (int j = 1; j <= value; j++) {

// if the number is divisible by 2 then it is even

if (j % 2 == 0) {

System.out.print(j + ” “);

}

}

}

}

Output :

Enter Limit:

40

Print Even numbers between 1 and 40

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40

2.How to Compare Two Numbers In Java ?

 package JavaPrograms_InterviewQuestions;

import java.util.Scanner;

public class CompareTwoNumbersInJava {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println(“Enter Number 1: “);

int num1 = sc.nextInt();

System.out.println(“Enter Number 2: “);

int num2 = sc.nextInt();

if (num1 > num2) {

System.out.println(num1 + ” is greater than ” + num2);

} else if (num1 < num2) {

System.out.println(num1 + ” is less than ” + num2);

} else {

System.out.println(num1 + ” is equal to ” + num2);

}

}

}

Output :

Enter Number 1:

14

Enter Number 2:

12

14 is greater than 12

3.Access Modifiers in Java ?

package JavaPrograms_InterviewQuestions;

public class Java_AccessModifier {

public int a = 10;

protected int b = 20;

private int c = 30;

int d = 40;

public void test1() {

System.out.println(“public test1()”);

}

protected void test2() {

System.out.println(“protected  test2()”);

}

void test3() {

System.out.println(“void test3()”);

}

private void test4() {

System.out.println(“private  test4()”);

}

public static void main(String[] args) {

Java_AccessModifier obj = new Java_AccessModifier();

obj.test1();

obj.test2();

obj.test3();

obj.test4();

System.out.println(obj.a);

System.out.println(obj.b);

System.out.println(obj.c);

System.out.println(obj.d);

}

}

Output :

public test1()

protected  test2()

void test3()

private  test4()

10

20

30

40

HOW TO HANDLE FRAME IN SELENIUM WEBDRIVER USING JAVA?

4.Verify Year is Leap Year or Not in Java ?

package JavaPrograms_InterviewQuestions;

import java.util.Scanner;

public class VerifyLeapYear {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println(“Enter Year: “);

int year = sc.nextInt();

// if year is divisible by 4, it is a leap year

if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {

System.out.println(“Year ” + year + ” is a leap year”);

} else {

System.out.println(“Year ” + year + ” is not a leap year”);

}

}

}

 

Note: Every fourth year is Leap Year.

Output :

Enter Year:

2008

Year 2008 is a leap year

 

5. To verify whether the no is palindrome or not in java?

package JavaPrograms_InterviewQuestions;

import java.util.Scanner;

class VerifyPalindrome {

public static void main(String args[]) {

int r, sum = 0, temp;

Scanner sc = new Scanner(System.in);

System.out.println(“Enter Number to Check Palindrome or Not:”);

int n = sc.nextInt();

temp = n;

while (n > 0) {

r = n % 10;

sum = (sum * 10) + r;

n = n / 10;

}

if (temp == sum) {

System.out.println(“Given Number is Palindrome”);

} else {

System.out.println(“Given Number is Not Palindrome “);

}

}

}

 

Note: A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed.

 

Output :

Enter Number to Check Palindrome or Not:

5

Given Number is Palindrome

STEPS TO CONFIGURE SELENIUM WEBDRIVER WITH JAVA TO DEVELOP TEST SCRIPTS

6. Find Factorial of a Number in Java?

package JavaPrograms_InterviewQuestions;

import java.util.Scanner;

public class FactorialNumber {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println(“Enter Number: “);

int value = sc.nextInt();

int factorial = value;

for (int a = (value – 1); a > 1; a–) {

factorial = factorial * a;

}

System.out.println(“Factorial of number is ” + factorial);

}

}

 

Note : factorial of number 4!= 4*3*2*1=24.

Output :

Enter Number:

4

Factorial of number is 24

7.H ow to Display Fibonacci Series in java?

 

package JavaPrograms_InterviewQuestions;

import java.util.Scanner;

public class FibonacciSeries {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println(“Enter Number for Fibonacci series :”);

int n = sc.nextInt();

int t1 = 0, t2 = 1;

System.out.print(“First ” + n + ” terms: “);

for (int i = 1; i <= n; ++i) {

System.out.print(t1 + ” , “);

int sum = t1 + t2;

t1 = t2;

t2 = sum;

}

}

}

 

Output :

Enter Number for Fibonacci series :

10

First 10 terms: 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 ,

8. To Check Whether a Number is Prime or Not in java?

 package JavaPrograms_InterviewQuestions;

import java.util.Scanner;

public class PrimeNumber {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println(“Enter Prime Number: “);

int num = sc.nextInt();

boolean flag = false;

for(int i = 2; i <= num/2; ++i)

{

// condition for nonprime number

if(num % i == 0)

{

flag = true;

break;

}

}

if (!flag)

System.out.println(num + ” is a prime number.”);

else

System.out.println(num + ” is not a prime number.”);

}

}

 

Output :

Enter Prime Number:

29

29 is a prime number.

 

 

 

SELENIUM

SELENIUM:

 
It is main purpose for testing and automate the web applications. It is not limited. Some of the broad web browsers publishers use on take steps for make it native part. This is the core technology in other browsers framework, automation tools and APIs. In web applications software testing framework is portable for it. In test scripting language SELENIUM provides a playback tool. It is no need for learning the script. If you write any tests it is offers test domain language. It contains the courses Scale, Ruby, Python, Groovy, Java, Perl, and PHP. These language set up Windows, Linux and micros platform. Without charge we can download and use it. Under Apache 2.0 License open source software released.
 
 SELENIUM

SELENIUM WEB DRIVER:

 
our topic used for web applications. In this programming language it used for automatic interact with the web browser to do any task. In this SELENIUM WEB DRIVER we discuss about the tools and features. Using  Firebug and IDE we can build a few scripts. These scripts moved and locating the strategies. Into the different type of web elements.
 

 FEATURES:

 
-Google Chrome
 
– Mozilla Firefox
 
– Html Unit
 
– Android Drive
 
– Internet Explorer
 
-Opera

SELENIUM COMPONENTS:

 
1. Web Driver:
 
It is very useful for Selenium. Commends are send to the browser and agreed by this. Browser-Specific driver achieved by this content. Commands are sending to a Browsers and gets results. Most of programming applications are launch and access by the browser drives. It is  known as HTML Unit browser. the driver encouraged to browse using HMTL Unit.
 
2. IDE:
 
It is complete information about integrated development environment for testing the tests. It extended through Add on Firefox and let on editing recording and testing on debugging. Recorder known as Selenium IDE. Selenium IDE is suitable with Selenium RC and it is little maintain and put down.
 
3. client API:
 
In different programming language can written in  tests an alternative written tests. This is discuss with Selenium in different methods. our programming language offers programming languages are Python, Java, Ruby, Java script and C++.
 
4. Remote Control:
 
It is also called as Selenium RC. Which agreed the commands in HTTP. It written in Java. Automated test written in web applications programming language by using RC. It is very useful for promoting Selenium Unit test frameworks.
 
5.  Grid:
 
It is a web server. Selenium grid used for remote machine running and it agreed for tests in web browser. One web server act as a hub. To know about the access on browser instance tests contact the hub. Hub has a list of web servers that provides browser instance used as tests and access.
Some programming languages are given below:
 
· Java
 
· C++
 
· Pearl
 
· PHP
 
· Python
 
· Ruby
 ADVANTAGES ON SELENIUM WEB DRIVER:
 
-It is not supported to extend the functionality and features.
 
-Not important for start executing scripts to start server prior.
 
-Testing on iPhone and android servers is support to add.
 
-Extracting the objectives is big problem. It is get to extract the object bulk.
 
-AJAX- based, Drag and Drop, and page navigation based UI elements used to support the features.
 
-Coordinates of any objects we can find through the web driver.
 
-Web driver can coordinate with testing frame work like Testing and J unit.
 
-It offers and improves reliability between browsers.
 
– Implementation of listeners is most important feature.
Ajax controls using Web Driver | Best Selenium Training Institute in Hyderabad

Best Selenium Training Institute in Hyderabad

 

Ajax controls using Web Driver

 

 
AJAX stands for Asynchronous JavaScript. It allows the Web page to retrieve amounts of data from the server without reloading the entire page. In AJAX has driven web applications, data from the server without refreshing the page.

 

We provide  Best Selenium Training Institute in Hyderabad by Real time Experts. We Offer Selenium live project

 

When we perform any action on Ajax controls. It using Wait commands will not work as the page is not actually refreshed here. Pausing the test execution using threads for a certain period of time. It is also not a good approach as web element. They earlier than the period of time depending on the system’s responsiveness, load. The other uncontrolled factors of the moment lead to test failures.

 

The best approach would be to wait for the required element in a dynamic period. Then continue the test execution as soon as the element found/visible manner.

 

This can do achieved with WebDriverWait in combination with the Expected condition. The best way to wait for an element checking for the condition in the script condition.

 

There are many methods which are available to use with wait.until.
The below are the few use testing an application

 

Syntax:

 

WebDriverWait wait = new WebDriverWait(driver, waitTime);

wait.until(ExpectedConditions.presenceOfElementLocated(locator));

The above statement will check for the element presence on the DOM of a page. This does not mean that the element is visible.

 

Syntax:

 

WebDriverWait wait = new WebDriverWait(driver, waitTime);

wait.until(ExpectedConditions.visibilityOfElementLocated(locator));

The above syntax will for the element present in the DOM of a page is visible.
Sometimes We need to check the element is invisible or not.
 

Syntax:

 

WebDriverWait wait = new WebDriverWait(driver, waitTime);

wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));

an exception as org.openqa.selenium.WebDriverException. The element is not clickable at the point. Another element would receive the click The below one use to wait for the element to be clickable.

Syntax:

 

WebDriverWait wait = new WebDriverWait(driver, waitTime);

wait.until(ExpectedConditions.elementToBeClickable(locator));

 

Best Selenium Training Institute in Hyderabad

 

Best Selenium Training Institute in Hyderabad: We provide  Best Selenium Training Institute in Hyderabad by Real time Experts. We Offer Selenium live project