Selenium 2.0 WebDriver – A Test Automation example
2014-09-24 10:25
429 查看
http://selftechy.com/2011/07/24/selenium-2-0-webdriver-a-test-automation-example
Getting started with webdriver requires some idea on “how to use the APIs” provided by the Selenium 2 Webdriver. Some of the useful APIs were discussed in detail in “Useful
APIs of WebDriver”. Example of automating a web page with these APIs explains the Test Automation with WebDriver better.
I have created a sample web page which has different objects such as edit box, labels, links, buttons, radio button, checkbox, javascript popup, etc. Please download the same from the below link:
Unzip the file into local drive (C:\, D:\,etc)
Sample WebSite is used in the below example:
Above example showcases all the basic operations of Test Automation through Selenium 2 WebDriver. Try to replicate the code and execute.
This should fill up the form, clicks save & closes the popup, and then navigates to the other page and comes back by clicking the links on the page.
http://selftechy.com/2011/07/24/selenium-2-0-webdriver-a-test-automation-example
Getting started with webdriver requires some idea on “how to use the APIs” provided by the Selenium 2 Webdriver. Some of the useful APIs were discussed in detail in “Useful
APIs of WebDriver”. Example of automating a web page with these APIs explains the Test Automation with WebDriver better.
I have created a sample web page which has different objects such as edit box, labels, links, buttons, radio button, checkbox, javascript popup, etc. Please download the same from the below link:
Unzip the file into local drive (C:\, D:\,etc)
Sample WebSite is used in the below example:
package com.selftechy.wdriverbasics; /* * Author - Seetaram Hegde * Copyrights - All rights reserved. * */ import java.util.List; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class CreateAccount { public static WebDriver driver; public Alert alert; @BeforeClass public static void setUpBeforeClass() throws Exception { driver=new FirefoxDriver(); driver.get("file:///C:/Sample%20Website/CreateAccount.htm"); } @Test public void testCreateAccount() throws InterruptedException{ navigatetoWebpage(driver,"file:///C:/Sample%20Website/CreateAccount.htm"); typeinEditbox(driver,"name","FirstName","Karthik"); typeinEditbox(driver,"name","Lname","Shetty"); typeinEditbox(driver,"xpath","//textarea[@name='street']","No. 425, 3rd Main, 7th Cross, 1st Sector, HSR Layout"); selectValue(driver,"Chennai"); selectValue(driver,"Tamilnadu"); selectValue(driver,"Germany"); selectRadiobutton(driver,"name","male"); selectCheckbox(driver,"name","kannada","ON"); selectCheckbox(driver,"name","english","ON"); selectCheckbox(driver,"name","hindi","ON"); clickButton(driver,"name","Save"); closeJscriptPopup(driver,alert); Thread.sleep(50); clickLink(driver,"xpath","//a[@href='CustomerInfo.htm']"); Thread.sleep(50); clickLink(driver,"xpath","//a[@href='CreateAccount.htm']"); } public static void closeJscriptPopup(WebDriver driver, Alert alert){ alert = driver.switchTo().alert(); alert.accept(); } public static void navigatetoWebpage(WebDriver driver, String url){ driver.get(url); } public static void clickButton(WebDriver driver, String identifyBy, String locator){ if (identifyBy.equalsIgnoreCase("xpath")){ driver.findElement(By.xpath(locator)).click(); }else if (identifyBy.equalsIgnoreCase("id")){ driver.findElement(By.id(locator)).click(); }else if (identifyBy.equalsIgnoreCase("name")){ driver.findElement(By.name(locator)).click(); } } public static void clickLink(WebDriver driver, String identifyBy, String locator){ if (identifyBy.equalsIgnoreCase("xpath")){ driver.findElement(By.xpath(locator)).click(); }else if (identifyBy.equalsIgnoreCase("id")){ driver.findElement(By.id(locator)).click(); }else if (identifyBy.equalsIgnoreCase("name")){ driver.findElement(By.name(locator)).click(); }else if (identifyBy.equalsIgnoreCase("name")){ driver.findElement(By.linkText(locator)).click(); }else if (identifyBy.equalsIgnoreCase("name")){ driver.findElement(By.partialLinkText(locator)).click(); } } public static void typeinEditbox(WebDriver driver, String identifyBy, String locator, String valuetoType){ if (identifyBy.equalsIgnoreCase("xpath")){ driver.findElement(By.xpath(locator)).sendKeys(valuetoType); }else if (identifyBy.equalsIgnoreCase("id")){ driver.findElement(By.id(locator)).sendKeys(valuetoType); }else if (identifyBy.equalsIgnoreCase("name")){ driver.findElement(By.name(locator)).sendKeys(valuetoType); } } public static void selectRadiobutton(WebDriver driver, String identifyBy, String locator){ if (identifyBy.equalsIgnoreCase("xpath")){ driver.findElement(By.xpath(locator)).click(); }else if (identifyBy.equalsIgnoreCase("id")){ driver.findElement(By.id(locator)).click(); }else if (identifyBy.equalsIgnoreCase("name")){ driver.findElement(By.name(locator)).click(); } } public static void selectCheckbox(WebDriver driver, String identifyBy, String locator, String checkFlag){ if (identifyBy.equalsIgnoreCase("xpath")){ if ((checkFlag).equalsIgnoreCase("ON")){ if (!(driver.findElement(By.xpath(locator)).isSelected())){ driver.findElement(By.xpath(locator)).click(); } } }else if (identifyBy.equalsIgnoreCase("id")){ if ((checkFlag).equalsIgnoreCase("ON")){ if (!(driver.findElement(By.id(locator)).isSelected())){ driver.findElement(By.id(locator)).click(); } } }else if (identifyBy.equalsIgnoreCase("name")){ if ((checkFlag).equalsIgnoreCase("ON")){ if (!(driver.findElement(By.name(locator)).isSelected())){ driver.findElement(By.name(locator)).click(); } } } } public static void selectValue(WebDriver driver, String valToBeSelected){ List <WebElement> options = driver.findElements(By.tagName("option")); for (WebElement option : options) { if (valToBeSelected.equalsIgnoreCase(option.getText())){ option.click(); } } } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("Execution completed....."); //driver.quit(); //if you want to stop the webdriver after execution, then remove the comment } }
Above example showcases all the basic operations of Test Automation through Selenium 2 WebDriver. Try to replicate the code and execute.
This should fill up the form, clicks save & closes the popup, and then navigates to the other page and comes back by clicking the links on the page.
相关文章推荐
- Selenium用户指南 - 第四章 Selenium 2.0和WebDriver[3]
- Selenium用户指南 - 第四章 Selenium 2.0跟WebDriver[2]
- Using Selenium 2 ( WebDriver ) to run qUnit test page
- Selenium2.0之WebDriver学习总结(1)
- Selenium2.0 WebDriver基本操作
- Selenium用户指南 - 第四章 Selenium 2.0和WebDriver[4]
- Selenium2.0之WebDriver学习总结(2)
- Selenium用户指南 - 第四章 Selenium 2.0和WebDriver[4]
- Selenium用户指南 - 第四章 Selenium 2.0和WebDriver[3]
- Selenium 2.0 WebDriver with Visual Studio, C#, & IE
- 【转载】Selenium2.0 WebDriver入门指南
- How to do web automation test using Selenium
- Selenium用户指南 - 第四章 Selenium 2.0跟WebDriver[4]
- Selenium2.0 WebDriver入门指南
- Selenium用户指南 - 第四章 Selenium 2.0和WebDriver[1]
- Selenium用户指南 - 第四章 Selenium 2.0跟WebDriver[1]
- Selenium用户指南 - 第四章 Selenium 2.0跟WebDriver[3]
- Selenium2.0之WebDriver学习总结(1)
- Selenium2.0介绍——WebDriver两种驱动浏览器的方式.
- Selenium2.0之WebDriver学习总结(1)