selenium IDE和eclipse环境搭建(用java开发)

selenium IDE和eclipse环境搭建
1.下载Firefox,最好路径是默认安装的。
2.官网下载selenium :http://docs.seleniumhq.org/download/
   选择自己想要的IDE,点Download latest released version后面的版本号就可以了。
   然后安装所有的插件。
   安装完之后在Firefox-->工具  最下方有一个Selenium IDE 这说明selenium已经成功安装。
3.下载java的selenium-server-standalone-2.37.0.jar (selenium RC )
   然后在eclipse上选好的文件下右键,选Build Path>>Add External Archives把selenium-server-standalone-2.37.0.jar文件选中上传。
   selenium-java.jar:在官网下载(http://docs.seleniumhq.org/download/ )  
4.当我们点开selenium IDE的窗口的时候我们会看到有录制的按钮
  在selenium IDE->>Options下面选项“Enable experimental features”前面打勾。
5.打开selenium IDE(默认开启录制),访问一个www.baidu.com,点击音乐,
  输入test进行搜索,然后点击完成按钮,完成录制,这个时候我们会到看到一个Command等相关的图,
  这个时候我们再去选Options->>Format->>Java/Junit4/WebDriver,将刚刚录制过程生成对应的java代码。
6.使用eclipse执行测试
  创建一个project,根据上一步骤转换的代码,创建包com.example.tests,在包下面创建一个class,baiduSearch.java
  添加之前下载的jar包:项目上右键 -> Build Path -> Configure Build Path.Java Build Path -> Libraries -> Add External JARS...
                                ->选择selenium-Java解压的JAR包和selenium-server-standalone.jar包 ->open -> ok
  添加完成后,存在下列:
  Junit-4.10.jar  :在我们下载的junit 4 压缩包里。
  Selenium-java-client-driver.jar:在我们下载的selenium-remote-control-1.0.3文件夹下。
  .(….\selenium-remote-control-1.0.3\selenium-java-client-driver-1.0.1\) 
   Selenium-server.jar :在我们下载的selenium-remote-control-1.0.3文件夹下。
   …..\selenium-remote-control-1.0.3\selenium-server-1.0.3\
   之后将录制代码导出为junit 4 类型的代码,我这里保存为test.java 并复制到我的项目中
   下面要启动服务:
   开始—运行—cmd  打开命令提示符。
  定位到…selenium-remote-control-1.0.3\selenium-server-1.0.3> 目录下。
   输入:java -jar selenium-server.jar  回车。服务就启动了。
   然后就可以运行测试了:右键项目 -> Run as ->JUnit Test ,若成功就会弹出Firefox浏览器执行测试。
   (写一个批处理,完成上面的工作。打开一个记事本,输入java -jar selenium-server.jar命令。保存为 .bat文件。下次双击这个文件就启动了。)
7.路径错误:重新安装Firefox到默认路径,也可以在path下增加Firefox的安装路径,
  或修改码,在DefaultSelenium方法中添加Firefox的安装路径即可,如下:
  @Before
   public void setUp() throws Exception {
   selenium = new DefaultSelenium("localhost", 4444, "*firefox  D:\\dev\\Firefox\\firefox.exe", "https://www.baidu.com/");
   selenium.start();
   }

   转换后的代码:
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;
    }
  }
}

猜你喜欢

转载自blog.csdn.net/qq_41444640/article/details/81739926