Selenium Web Driver Best Practices

13
Selenium WebDriver Best Practices Presenter: Rajendra Narayan Mahapatra, Mindfire Solutions Date: 25/06/2014

description

WebDriver is the most efficient way for automating applications using selenium. This presentation focuses on the best practices on WebDriver.

Transcript of Selenium Web Driver Best Practices

Page 1: Selenium Web Driver Best Practices

Selenium WebDriver Best Practices

Presenter: Rajendra Narayan Mahapatra, Mindfire SolutionsDate: 25/06/2014

Page 2: Selenium Web Driver Best Practices

Presenter: Rajendra Narayan Mahapatra, Mindfire Solutions

About Me :

Total Experience:- 3.3 years

Previous Companies: Qualitree Solutions Pvt. Ltd. PointCross.com Pvt. Ltd.

Certification:- Vskills(VS-1083) Selenium Certification

Skills :- Selenium RC, Selenium WebDriver, QTP, Manual Testing,SQL

Page 3: Selenium Web Driver Best Practices

Presenter: Rajendra Narayan Mahapatra, Mindfire Solutions

AGENDA / Topic INDEX

--> Wait Commands 1. Implicit Wait 2. Explicit Wait

--> How to handle alerts --> How to switch to window --> How to retrieve Text of all the links --> How to do Mouse Over action--> How to do Drag & Drop action--> How to scroll to Element--> How to take snapshot --> How to read pdf contents

Page 4: Selenium Web Driver Best Practices

Presenter: Rajendra Narayan Mahapatra, Mindfire Solutions

--> Wait Commands 1. Implicit Wait 2. Explicit Wait

Implicit Wait------------------Implicit Wait will wait for whole DOM (Document Object Model) to be loaded for the specified time. If not then it wil thoroe timeout exception.

e.g. driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS)

Explicit Wait------------------Explicit Wait will wait for certain condition to occur.

Page 5: Selenium Web Driver Best Practices

Presenter: Rajendra Narayan Mahapatra, Mindfire Solutions

How to handle Alerts----------------------------

// Wait For Alert To ComeWebDriverWait wait = new

WebDriverWait(driver,30);Alert alert =

wait.until(ExpectedConditions.alertIsPresent());

// Get the Text From the AlertString alertText = alert.getText();System.out.println("Alert Text :"+alertText);

alert.accept();

Or

alert.dismiss();

Page 6: Selenium Web Driver Best Practices

Presenter: Rajendra Narayan Mahapatra, Mindfire Solutions

How to switch to Window---------------------------------

// Store the CurrentWindow for future reference

String currentWindow = driver.getWindowHandle();String popupWindowHandle = null;

// Switch To Popup Window

for(String handle : driver.getWindowHandles()){if(!handle.equals(currentWindow)){

popupWindowHandle = handle;

}}

driver.switchTo().window(popupWindowHandle);

Page 7: Selenium Web Driver Best Practices

Presenter: Rajendra Narayan Mahapatra, Mindfire Solutions

How to scroll to element-----------------------------------------

// Assign Object for Last TrainWebElement lastTrain =

driver.findElement(By.xpath("//*[@id='divTrainsListTrainsObj']/table[1]/tbody/tr[27]/

td[2]/a"));

// Scroll to Last Train

Coordinates coordinate = ((Locatable) lastTrain).getCoordinates();

coordinate.inViewPort();

How to take snapshot------------------------------// Take ScreenShot

File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(scrFile, new File("D:\\selenium\\screenshot.png"),

true);

Page 8: Selenium Web Driver Best Practices

Presenter: Rajendra Narayan Mahapatra, Mindfire Solutions

How to retrieve Text of all the links

-----------------------------------------// Verify Links

List<WebElement> listOfLinks = driver.findElements(By.tagName("a"));

String linkText[] = new String[listOfLinks.size()];int i=0;for(WebElement l1: listOfLinks){

// Get Link TextlinkText[i] = l1.getText();System.out.println(linkText[i]);i++;

}

// Click on Links

for(String t : linkText){

driver.findElement(By.linkText(t)).click();

if(driver.getTitle().contains("Under Construction")){System.out.println(t+" : Link is under construction");

}

driver.navigate().back();}

Page 9: Selenium Web Driver Best Practices

Presenter: Rajendra Narayan Mahapatra, Mindfire Solutions

How to read PDF contents

-----------------------------------------// Required JAR files

1. fontbox-1.8.5.jar2. pdfbox-1.8.5.jar

// Read PDF ContentsPDDocument pd;pd = PDDocument.load(new File("D:\\selenium\\VS-1083_Certified

Selenium Professional_Reading_Material.pdf"));System.out.println("Total Number Of pages :"+pd.getNumberOfPages());

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

Page 10: Selenium Web Driver Best Practices

Presenter: Rajendra Narayan Mahapatra, Mindfire Solutions

Question and Answer

Page 11: Selenium Web Driver Best Practices
Page 12: Selenium Web Driver Best Practices

Presenter: Rajendra Narayan Mahapatra, Mindfire Solutions

Thank you