- Selenium IDE Demo - Quick Beginner's Tutorial mentions the Firefox plugin which records and plays tests. Selenium IDE is useful, you can easily save and replay tests as html files, but i want my tests written in java and want to run them as junit tests.
- The following three videos by Absoft Training are great!!
- Selenium Tutorial - Starting with Selenium WebDriver – Part 1 mentions WebDriver interface and illustrates to write basic test in java. It also shows how to use it for cross browser testing.
- Selenium Tutorial - Starting with Selenium WebDriver – Part 2 explains WebElement in detail and shows how to locate an element on the web page.
- Using XPath to Locate Web Elements - in very detail shows how to locate an element by xpath.
Absoft Training presents more tutorials here: http://www.absofttrainings.com/selenium-training-free-videos/
My usage tips :
My usage tips :
- Download Selenium standalone server and the java client.
- In Eclipse create a java project and add the downloaded jars (must be 2 jars) to classpath.
- Download Selenium IDE and install it as Firefox plugin.
- Run the server and deploy your application to test.
- Open Firefox, start Selenium IDE, press record button and enter your web application url.
- Test your application, one use case at a time.
- Close Selenium recording, save it and export test case as Java / Junit 4/ WebDriver.
- I did not use the immediately exported files. But, i got help from them. I think preparing a utility file which enables you to switch between browser types, between development and production environments is convenient. If you have a complicated web page, it is good practice to write find methods for web elements and reuse them.
- Then run tests as junit.
Some technical tips:
- WebDriver interface provides many methods including findElement(), getTitle(), close(), getCurrentUrl() etc. It is good to examine its javadoc.
- You can locate a web element by id, name or xpath.
- To create Firefox driver, simply :
- WebDriver driver = new FirefoxDriver();
- To create Chrome driver, download the third party executable
- System.setProperty("webdriver.chrome.driver", "path_to_executable");
- WebDriver driver = new ChromeDriver();
- To create IE driver, download the third party executable
- System.setProperty("webdriver.ie.driver", "path_to_executable");
- WebDriver driver = new InternetExplorerDriver();
- To make same arrangements:
- driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
- driver.manage().window().maximize(); // maximizes opening browser
- Selenium sees everything on a web page as web element.
- findElement retrieves first one that matches
- findElements retrieves all elements that match
- You can locate by
- Id
- Name
- LinkText (text of hyperlink)
- PartialLinkText
- Xpath (ultimate solution, when others don't work)
- ByXpath("//input[@placeholder='Email']") means locate an input field whose placeholder attribute value is Email.
- CssSelector
- TagName
- ClassName
- Locating by Xpath
- Xpath-Absolute: not recommended.
- //html/body/tag1[index]/..
- Xpath-Attributes: best when can't use id or name
- //input[@id='Pass' and @placeholder='Password']
- //input[contains(@id, 'user')]
- //input[starts-with(@id, 'user')]
- Selenium IDE and Firefox developer tools can help to get the xpath of the element.
- Xpath-Relative: best for table cells
- //table[@id='xtable']/tbody/tr[3]/td[2]
- You can make Selenium to wait for the presence of some element:
- final WebDriverWait wait = new WebDriverWait(driver, 60);
- wait.until(ExpectedConditions.presenceOfElementLocated(By.id("the_id")));
// create driver for the browser you want
WebDriver driver = new FirefoxDriver();
//open homepage
driver.get("the_url");
//find related field, enter some value
driver.findElement(By.[id|name|xpath]("the_value")).[clear()|click()|sendKeys("..")];
//find command button and click
driver.findElement(By.id("the_id")).click();
//assert results
WebDriver driver = new FirefoxDriver();
//open homepage
driver.get("the_url");
//find related field, enter some value
driver.findElement(By.[id|name|xpath]("the_value")).[clear()|click()|sendKeys("..")];
//find command button and click
driver.findElement(By.id("the_id")).click();
//assert results