Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Export Selenium IDE Script to other Languages

When recording test case with Selenium IDE, the default script will be html script. The script can be exported to other programming languages, so that QA engineers can use Java, C#, Ruby, or Python to develop Selenium test framework.

 

To change the script format, open "Options" menu, select "Format" command, and then choose one of these programming languages from the menu.

 

For example, you select Java/JUnit4/Webdriver, Selenium IDE will display a warning. Just click Ok.

The "source" will be converted to Java/JUnit4/WebDriver Code. You can copy the code into Java development console like Eclipse or Intellj, modify some code and then execute it as JUnit test.

Below is the code after importing it into Eclipse as JUnit test.

package com.seleniummaster.logintest;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SeleniumMasterLoginTest {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.seleniummaster.com";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testSeleniumMasterLogin() throws Exception {
    driver.get(baseUrl + "/seleniummastertestapp/index.php");
    for (int second = 0;; second++) {
      if (second >= 60fail("timeout");
      try if (isElementPresent(By.cssSelector("img[alt=\"Selenium Master\"]"))) breakcatch (Exception e) {}
      Thread.sleep(1000);
    }

    driver.findElement(By.id("login_login_username")).clear();
    driver.findElement(By.id("login_login_username")).sendKeys("test");
    driver.findElement(By.id("login_login_password")).clear();
    driver.findElement(By.id("login_login_password")).sendKeys("xxxx");
    driver.findElement(By.id("login_submit")).click();
    assertEquals("Online users", driver.findElement(By.cssSelector("#sb-onlineusers > 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;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    catch (NoAlertPresentException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      else {
        alert.dismiss();
      }
      return alertText;
    finally {
      acceptNextAlert = true;
    }
  }
}