WebDriver Cheatsheet

My cheat sheet for frequently WebDriver commands:

To initiate browsers:


1
2
3
4
driver = new FirefoxDriver();
driver = new InternetExplorerDriver();
driver = new EdgeDriver();
driver = new ChromeDriver();


Various operations for an element:

Most used operations for WebElements element:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
//import org.openqa.selenium.WebElement;

WebElement webE = driver.findElement(By.name("String name"));
webE.findElement(By.name("String name"));
webE.click();
webE.clear();
webE.sendKeys("some text");
webE.submit();
webE.getText();
webE.getAttribute("String attribute");
webE.isDisplayed();
webE.isSelected();
webE.isEnabled();
webE.toString();


Most used operations for a list/ drop-down:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
//import org.openqa.selenium.support.ui.Select;

Select select = new Select(webE);
select.toString();
select.selectByIndex(1);// int of the index
select.selectByValue("String value");
select.selectByVisibleText("String text");
select.deselectAll();
select.deselectByIndex(1);
select.deselectByValue("String value");
select.deselectByVisibleText("String text");
//method to list all selected options from a list
select.getAllSelectedOptions();
//method to list all available options in a list
select.getOptions();

Most used operations for WebElements element:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//import org.openqa.selenium.WebDriver;

WebDriver webD = new FirefoxDriver();
webD.findElement(By.id("String id"));
webD.close();
//This is for listing elements from a list
webD.findElements(By.linkText("String linkText"));
webD.getTitle();
webD.get("String to get");
webD.getCurrentUrl();
webD.getPageSource();
webD.getWindowHandle();
//Lists all the handles when there are multiple windows
webD.getWindowHandles();
webD.quit();
webD.switchTo();
webD.toString();

//manage operations
webD.manage().logs();
webD.manage().window().maximize();
webD.manage().window().getSize();

//navigate operations
webD.navigate().refresh();
webD.navigate().forward();
webD.navigate().back();
webD.navigate().to("String URL");

//manage alerts
webD.switchTo().alert().getText();
webD.switchTo().alert().accept();
webD.switchTo().alert().dismiss();
webD.switchTo().alert().sendKeys("String keys to send");


To get and compare the window/ page title:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
//Get the title of the window/ page
String actualTitle = driver.getTitle();

//Compare the actual title with the expected title 
if (actualTitle.equals(expectedTitle))
{
 System.out.println("actualTitle = expectedTitle");
}
else
{
 System.out.println("actualTitle != expectedTitle");
}


To switch to desired window if multiple windows are open:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
//Get current handle
String expectedWindow = driver.getWindowHandle();
// or if the handle is known then:
//String expectedWindow = "String expectedWindow Handle";

//Collect all handles
Set<String> handles = webD.getWindowHandles();
//This will iterate thru the list of all open windows till both handles match
for(String handle : handles){
 if(!handle.equals(expectedWindow)){
  webD.switchTo().window(handle);
 }   
}


To find element on page using findElement method:

1
2
3
4
5
6
7
8
driver.findElement(By.className("String className"));
driver.findElement(By.cssSelector("String selector"));
driver.findElement(By.id("String id"));
driver.findElement(By.linkText("String linkText"));
driver.findElement(By.name("String name"));
driver.findElement(By.partialLinkText("String linkText"));
driver.findElement(By.tagName("String name"));
driver.findElement(By.xpath("String xpathExpression"));


To find element on page using By method:

1
2
3
4
5
6
7
8
9
By byId=By.id("String id");
By byName = By.name("String name");
By byXpath = By.xpath("String xpath");
By byClassName = By.className("String classname");
By byCssSelector = By.cssSelector("some css selector");
By byLinkText = By.linkText("some linkText");
By byPartialLinkText = By.partialLinkText("some partial linkText");
By byTagName = By.tagName("some tag name");
Class<By> byClass = By.class;


To verify if the checkbox/radio is checked or not:

1
2
3
4
5
6
7
8
if (driver.findElement(By.xpath("xpath of the checkbox/radio button")).isSelected())
{
 System.out.println("Radio button is selected");
}
else
{
 System.out.println("Radio button is selected");
}


To handle alert pop-ups - Accept, Cancel, Switch back to main window:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
//Get the main window handle
String mainWindowHandle = driver.getWindowHandle();

//Move control to alert pop-up
Alert alt = driver.switchTo().alert(); 

//Accept alert pop-up
alt.accept();

//Cancel alert pop-up
alt.dismiss();

//Move the control back to main web window
driver.switchTo().window(mainWindowHandle);


To right click on an element:

Method for right clicking:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public static void rightClick(WebElement element) {
 try {
  Actions action = new Actions(driver).contextClick(element);
  action.build().perform();

  System.out.println("Right clicked the element successfully");
 } catch (Exception e) {
  System.out.println("Cannot right click the element"
    + e.getStackTrace());
 }
}

Call the above method:

1
2
3
4
5
//Get the element to work on
WebElement webElement = driver.findElement(By.xpath("String xpathExpression"));

//Call method for right click
rightClick(webElement);


To drag and drop an element:

Method for right clicking:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public static void dragAndDrop(WebElement source, WebElement target){

 try{
  Actions action = new Actions(driver);
  action.dragAndDrop(source,target).perform();

  System.out.println("Dragged the element successfully");
 }catch (Exception e){
  System.out.println("Cannot drag n drop the element"
    + e.getStackTrace());   
 }
}

Call the above method:

1
2
3
4
5
6
7
8
//Get the source element to drag
WebElement source = driver.findElement(By.xpath("String xpathExpression"));

//Get the target drop element
WebElement target = driver.findElement(By.xpath("String xpathExpression"));

//Call method for drag n drop
dragAndDrop(source, target);


To upload a file:


1
2
3
4
//'upload element' is the elemet on page where you have to click to upload the file
//'Location of file' is the location of file to be uploaded
driver.findElement(By.xpath("upload element")).sendKeys("location of file");
 



To scroll up and down the page:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 public class ScrollDown {

 public static void main(String[] args) throws InterruptedException {

 WebDriver driver = new FirefoxDriver();

 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

 driver.get("url of page to scroll");

 driver.manage().window().maximize();

 JavascriptExecutor jsx = (JavascriptExecutor)driver;

//scroll down, change value 4500 as per the requirment
 jsx.executeScript("window.scrollBy(0,4500)", ""); 

 Thread.sleep(3000);

//scroll up, change value 450 as per the requirment
 jsx.executeScript("window.scrollBy(450,0)", "");
 }
 }



To double click on an element:


1
2
3
4
5
 
Syntax- Actions act = new Actions(driver);
 
act.doubleClick(webelement);
 



To press Shift+tab:


1
2
3
4
5
 
String shiftTab = Keys.chord(Keys.SHIFT,Keys.ENTER);
 
webelement.sendKeys(shiftTab);
 



To check all check boxes on the page:


1
2
3
List<WebElement> chkBox = driver.findElements(By.xpath("String xpath"));
for(int i=0; i<=chkBox.size(); i++){
chkBox.get(i).click();



To count the number of links on a page:
Use the locator 'By.tagName' and find the elements for the tag ' //a' and then use loop to count the number of elements found like so:

1
2
3
4
int count = 0;
List<WebElement> link = driver.findElements(By.tagName("a"));
//this will print the number of links in a page.
System.out.println(link.size()); 




No comments:

Post a Comment