Moving from Selenium IDE to Java (JUnit) is more straightforward than
one could think! When you have your Selenium IDE test case recorded and
Eclipse (for Java) installed, simply follow these steps:
- Download Selenium Client & WebDriver Language Bindings from Selenium website
- Extract downloaded ZIP file to intended location (keep some permanent one, as you will need them for a long time)
- Create new Java project in Eclipse
- Add all extracted JARs (also these in lib folder) to Java Build Path in project Properties
- Create Java class
- Open recorded test case in Selenium IDE, click File » Export Test Case As » Java / JUnit4 / WebDriver
- Save this file in temporary location
- Copy it’s content to Java class created in one of previous steps
- Adjust package and class name, get rid of warnings – finally it should look similar to this one:
package com.example; import java.util.regex.Pattern; import java.util.concurrent.TimeUnit; import org.junit.*; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; public class SampleTest { private WebDriver driver; private String baseUrl; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { driver = new FirefoxDriver(); baseUrl = "http://www.google.pl/"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Test public void testZ() throws Exception { driver.get(baseUrl); driver.findElement(By.id("gbqfq")).clear(); driver.findElement(By.id("gbqfq")).sendKeys("selenium"); driver.findElement(By.id("gbqfb")).click(); driver.findElement(By.linkText("Downloads")).click(); assertEquals("Selenium Client & WebDriver Language Bindings",
driver.findElement(By.cssSelector( "a[name=\"client-drivers\"] > h3")).getText()); } @After public void tearDown() throws Exception { driver.quit(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } private boolean isElementPresent(By by) { try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } }
No comments:
Post a Comment