`

初次接触selenium+eclipse简单环境搭建

 
阅读更多

1.自己电脑上有Firefox,最好路径是默认安装的。

2.去selenium 官网下载http://docs.seleniumhq.org/download/

   选择自己想要的IDE,一般都是最新的,点Download latest released version后面的版本号就可以了。

   允许,然后安装所有的插件。

   安装完之后在Firefox->>工具  最下方有一个Selenium IDE 这说明selenium已经成功安装。

3.一般我们用selenium不仅仅是录制用到IDE,还会用到RC来查看代码,我自己用的是java,所以下载了java的selenium-server-standalone-2.37.0.jar,然后在eclipse上选好的文件下右键,选Build Path>>Add External Archives把selenium-server-standalone-2.37.0.jar文件选中上传。

4.当我们点开selenium IDE的窗口的时候我们会看看到有录制的按钮,要注意的是如果你想查看录制后的java

代码,选在selenium IDE->>Options下面给选项“Enable experimental features”前面打勾。

5.打开selenium IDE(默认录制按钮在打开的时候已经是开始的),访问一个www.baidu.com,点击音乐,输入test进行搜索,然后点击完成按钮,完成录制,这个时候我们会到看到一个Command等相关的图,

这个时候我们再去选Options->>Format->>Java/Junit4/WebDriver,确定以后会看到刚刚录制过成生成的对应java代码。

package com.example.tests;

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

  @Test
  public void testUntitled() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.linkText("音乐")).click();
    driver.findElement(By.id("ww")).sendKeys("test");
  }

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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics