This blog is for Automation Test Engineers

Sunday 3 September 2017

Locators

Before performing any action on the fields which are present in webpage the selenium has to uniquely identify those fields and that can be done with the help of locators.
 There are 8 eight type of locators:

1)ID:
The most efficient and preferred way to locate an element on the webpage is by ID.
This will be unique which can be easily identified. They are the most safest and fastest type of locators and always be the first choice. Unfortunately there are some cases where element doesn't have unique id. In these  cases we can choose other alternative locator.

2)Name
When there is no ID , the next worth locator can be name.
<button name="Submit">

3)LinkText
Finding an element with LinkText is very simple.If there are multiple links with same link text in such cases selenium will perform action on first matching element.
Eg:<a href="Seleniumhq.org">Downloads</a>


4)Partial LinkText
Both LinkText and Partial LinkText  can be used only if the webelement which has html tag "a".

5)TagName
Tagname can be used with group elements like select, checkboxes/dropdowns

6)ClassName
Finding an element with ClassName is also very simple

7)Xpath
xpath is path of element in Html tree. Inorder to derive the xpath expression we should have complete knowledge on HTML tree.The first forward slash (/) represents beginning of HTML tree which is called root.(Absolute xpath)All other forward slash represents immediate child(Relative xpath). Inorder to check whether xpath expression is correct or not we can use firepath addon which is an additional tab in the firebug window.

8)CSS Selector
CSS stands for Cascade Style Sheet. It is a type of locator with the following syntax:
Here we cant use ID, name or other locator, but we can use TagName locator but it will be duplicate. In such cases we can use CSS locator.
driver.findElement(By.cssselector("input[type='text']").sendkeys("xyz"));
driver.findElement(By.cssselector("input[type='password']").sendkeys("xyz"))







1

How to perform drag and drop



Actions actions=new Actions(driver);
WebElement sourceelement=driver.findElement(By.xpath(""));
WebElement targetelement=driver.findElement(By.xpath(""));
actions.dragAndDrop(sourceelement,targetelement).perform();



0

How to take data from external source like excel

WebDriver driver=new FirefoxDriver();
driver.get("https://www.facebook.com/");
WebElement username=driver.findElement(By.id("email"));
FileInputStream fis=new FileInputSteam("D:\\testsdata.xlsx");
WorkBook wb=WorkBoolFactory.create(fis);
Sheet s=wb.getSheet("sample");
Row r=s.getRow(2);
Cell c=r.getCell(0);
String v=c.getStringCellValue();
usernametxtbox.senkeys(v);
WebElement pwdtxtbox=driver.findElement(By.id("pass"));
Row r=s.getRow(3);
Cell c=r.getCell(1);
String v1=c.getStringCellValue();
pwdtxtbox.senkeys(v1);

}


0

For loop



We can use for loop when we know how many number of times it should repeat the iterations and also suppose if we want to print the value from 1 to 100 we can use for loop. Eg:

for(int i=0;i<100;i++){
System.out.println("The value of i:"+i);
}

Here for loop starts with keyword "for" which is in lowercase followed by 3 statements. Each Statements are separated by  semicolon. The following statements are:

1)Loop Initializer
2)Condition
3)Post Iterative statement

1)Loop Initializer:
This is the first statement where we are using variable i which is of type int and we have assigned the value 0 . This statement can be used to initialize the variable. This statement can be executed only once when the for loop starts.

2)Condition
This is second statement is evaluated to either true or false. Here this statements compares  the value of i. If the value of i is less than 100 then this statement will be executed. If value of i is not less than i then statement wouldn't be executed anymore and it will jumps back to previous statements.

3)Post Iterative Statements
This is the third statement. This statement is executed after each iteration of for loop. This statements can increment or decrement  the variable value.





1

How to use firepath addon

Inorder to check whether xpath expression is correct or not we use firepath addon which is an additional tab in firebug window. Inorder to install follow below steps:
 Go to Firefox addons page
 Search for fire path
Click on Install button in firepath
Close and reopen browser after installation.
Right click on any Element where you want to know the source code.

Go to inspect Element with Firebug option so that source code will be highlighted

0

Firebug in selenium webdriver

Inorder to install follow these steps:

Go to Mozilla Firefox browser
Go to Menu
Go to Add-ons
Go to Extensions
Search for firebug
Click on Install, close and reopen browser.

Go to required webpage right click on element, then select "inspect element with firebug"which will display source code in firebug window an it will highlight the source code of selected element.




0

How to takes screenshot

WebDriver driver=new FirefoxDriver();
driver.get("https://www.google.co.in/");
File scrfile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrfile,new File("E://test.png"));
driver.close();




}
0

How to read contents from pdf

Public class pdfdocument{

PDDocument pd;
pd=PDDocument.load(new File("C:\\test.pdf"));
System.out.print("total pages:"+ pd.getNumberofpages());

PDFTextStripper pdf=new PDFTextStripper ();
System.out.print( pdf.getText(pd));

}

0

To Check whether image is displayed or not




WebDriver driver=new FirefoxDriver();
driver.get("https://www.google.co.in");
WebElement image= driver.findElement(By.id("hplogo"));
Boolean status=image.isDisplayed();
if(status==true){
System.out.println("Image is displayed");
}
else{
System.out.println("Image is not displayed");
}

Output:true


0

How to find position of cursor in window

Pointerinfo c=Mouseinfo.getPointerinfo();
Point d=c.getLocation();
int a=(int)d.getA();
int b=(int)d.getB();

System.out.println(a+","+b);



}
1

How to find whether two string equal or not



String a="Selenium";
String b="Selenium";

System.out.println(a.equals(b));
}


0

How to find number is positive or negative


int d=-10;
if(d>0){
System.out.println("is positive number");
}
else{
System.out.println("is negative number");
}

Output: is negative number



0

How to find number is even or odd

int z=4;
if(z%2==0){
System.out.println("Its even no");
}
else{

System.out.println("Its odd no");


}
0

Factorial number



int a,b,fact=1;

int a=6;
if(a<0}{
System.out.println("number should be non negative number");
}
else{
for(b=1;b<=a;b++)
fact=fact*c;
System.out.print(fact);
}


0

Print square root from 1 to 10



int c=1,d=10;
while(c<=d){
System.out.println(c*c);
c++;
}


0

Saturday 2 September 2017

How to generate random numbers


int a;
Random b=new Random();
(for a=1;a<=10;a++){
System.out.println(b.nextInt(1000);
}

0

Swapping two numbers without using 3rd variable




int a=3;
int b=1;

a=a+b;//4
b=a-b;//3
a=a-b;//1
System.out.println("After Swapping the value of x will be:"+x "and y:" +y);

0

How to select random options in multiple select dropdown box and print them

WebDriver driver=new FirefoxDriver();
driver.get("https://www.facebook.com/");
WebElement day=driver.findElement(By.id("day"));
Select sel=new Select(box);
sel.selectByIndex(0);
sel.selectByIndex(1);
sel.selectByIndex(2);
List<WebElement>list=sel.getAllSelecetedOptions();

System.out.println(list.size());

0

How to know whether page is fully loaded


JavascriptExecutor js=(JavascriptExecutor)driver);
String name=(String)js.executeScript("alert(document.readstate");
System.out.print(name);


0

Search specified option present in list box

WebDriver driver=new FirefoxDriver()'
driver.get("https://www.facebook.com/");
WebElement Day=driver.findElement(By.id("day"));
Select sel=new Select(Day);
List<WebElement>list=sel.getOption();
System.out.println(list);
for(int i=0;i<list.size();i++){
String s=list.get(i).getText();
if(s.contains("16")){
System.out.print("found");
s.click();
break;//includes duplicate option 

}}

0

How to print all options in listbox in reverse order

WebDriver driver=new FirefoxDriver();
driver.get("https://www.facebook.com/");
WebElement box=driver.findElement(By.id("day");
Select sel=new Select(box);
List<WebElement>list=sel.getOption();
System.out.println(list);
for(int i>0;i<list.size()-1;i--){
System.our.println(list.get(i).getText());


}



}
0

Count no of options present in listbox

WebDriver driver=new FirefoxDriver();
driver.get("https://www.facebook.com/");
WebElement Day=driver.findElement(By.id("day");
Select sel=new Select(box);
List<WebElement>list=sel.getOption();
System.out.println(list);
for(int i=0;i<list.size();i++){
System.our.println(list.size());



}



}
0

How to print URL present in address bar


WebDriver driver=new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.getCurrentURL():


0

How to print title of browser title

WebDriver driver=new FirefoxDriver();
JavascriptExecutor js=(JavascriptExecutor )driver;
String browsertitle=(String)js.executeScript("return navigator.userAgent");
System.out.println(browsertitle);


0

How do you find the total no of objects in a webpage

 WebDriver driver=new FirefoxDriver();
driver.get("https://www.google.com");
List<WebElement>obj=driver.findElements(By.xpath("//*"));

System.out.println(obj.size());

0

How to click if a radio button is disable


WebDriver driver=new FirefoxDriver();
driver.get("https://www.facebook.com/");
((JavascriptExecutor)driver).executeScript("document.getElementByid("u_0_b").checked=false");


0

How do you use JavaScript in selenium

 Javascript is one of the programming language of web. All HTML pages are using Javascript.
To execute Javascript in our webdriver script which can use JavascriptExecutor.

JavascriptExecutor in interface which is available in package org.openqa.selenium.JavascriptExecutor.

Eg:
WebDriver driver=new FirefoxDriver();
driver.get("https://www.facebook.com/");
//this will execute Javascript in your script
((JavascriptExecutor)driver).executescript("document.getElementById("email").value="zyt@gmail.com");






0

How do you launch browser using webdriver

WebDriver driver=new FirefoxDriver();
WebDriver driver=new ChromeDriver();
WebDriver driver=new InternetExplorerDriver();




0

Tuesday 29 August 2017

Interview Questions for Selenium WebDriver


1)Tell me about yourself?
2)Tell me about your project?
3)Challenges faced in Selenium?
4)Types of locators?
5)What is polymorphism and tell me where have used in your project?
6)What is encapsulation and tell me where have used in your project?
7)Different annotations used in TestNG?
8)Challenges faced in your project?
9)How do you handle dynamic elements?
10)What is Dataprovider?
11)How do u handle dynamic elements?
12)Explain framework that u have used?




0

How to print the contents of excel file



FileInputStream fis=new FileInputStream("E:\\test.xlsx");
Workbook wb=new WorkbookFactory.create(fis);
Sheet s=wb.geSheet("Sheetname");
int rowcount=s.getLastRowNum();
for(int j=0;j<rowcount;j++){
Row r =s.getRow(j);
int columncount=r.getLastCellNum();
for(int a=0;a<columcount;a++){
Cell c=r.getCell(a);
String s=c .getStringCellValue();
System.out.println(s);
}


0

How to retrieve data from notepad

FileInputStream fis=new FileInputStream("xyz.txt");
int j=0;
while(j=fis.read()!=-1){
System.out.println(char)j);
}
fos.close();

}


0