Selenium+eclipse Construction and writing of the first Test code 2 (webdriver)

Source: Internet
Author: User
Tags gettext seleniumhq stringbuffer





The first thing to do is:



1, ECLIPSE+JDK



2, Selenium-ide (http://release.seleniumhq.org/)



3. Firefox



4, Selenium-java-2.53.1.jar (as if not necessary)



5, Selenium-server-standalone-2.53.1.jar



Selenium-ide using methods, selenium code conversion Java, and Eclipse creation I have another blog post that mentions: http://blog.csdn.net/songjiaping/article/details/49507177



Selenium Download Address:



Https://github.com/SeleniumHQ/selenium/releases



Http://selenium-release.storage.googleapis.com/index.html






Java/junit 4/remote Control



The code format before the Selenium-ide conversion is: Java/junit4/remote Control



This format runs in Eclipse and needs to add at least Selenium-server-standalone-2.53.1.jar in build path



And you need to run Selenium-server-standalone-2.53.1.jar to test when you run test





Java-jar Selenium-server-standalone-2.53.1.jar

Java/junit 4/webdriver





Using Java code in the above format to run in eclipse requires adding Selenium-server-standalone-2.53.1.jar in the build path



But you don't need to run the jar package when you run test, just click Run.






To view the various methods of selenium, you can add the corresponding version of source code:



Download Address: https://github.com/SeleniumHQ/selenium/releases/tag/2.53.1



Specific methods can be consulted: http://blog.csdn.net/songjiaping/article/details/49585639






1. Editing scripts with Selenium-ide



The following is to open Baidu home page, enter "Selenium", click to query, and finally verify that there is no expected to display the text "Selenium-web Browser Automation"









2. Conversion script Format-java/junit 4/webdriver









3. Copy generated code into eclipse new Java file with the same name



The eclipse path is as follows:






Drivertest.java for the newly created Java file, Selenium-server-standalone-2.53.1.jar must be added to the build path



The following is a copy to Drivertest.java code that needs to be modified with the package and class names





package selenium.test;

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 DriverTest {
  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 = "https://www.baidu.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testUntitled2() throws Exception {
    driver.get("https://www.baidu.com");
    // ERROR: Caught exception [ERROR: Unsupported command [setSpeed | 500 | ]]
    driver.findElement(By.id("kw")).clear();
    driver.findElement(By.id("kw")).sendKeys("selenium");
    driver.findElement(By.id("su")).click();
    try {
      assertEquals("Selenium - Web Browser Automation", driver.findElement(By.xpath("//div[@id='1']/h3/a[1]")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
  }

  @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;
    }
  }
}

Directly click Run to execute




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.