Selenium录制的脚本可以转换Java JUnit单元测试,可以使用Java项目管理工具Maven统一运行,从Selenium脚本中导出Java代码,按照如下步骤
使用Java运行Selenium脚本,使用Chome导出Java脚本必须下载Chome驱动,如果使用Firefix导出的Java脚本必须下载Firefix驱动。
Firefix驱动
链接:https://pan.baidu.com/s/1-JGr0W-IP_a5yOlsjnPJzg
提取码:erxh
导出的Java代码,需要添加到Java Maven项目中,并添加所需依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.5.3</version>
</dependency>
修改Java脚本,需要设定Firefox驱动所在目录
System.setProperty(“webdriver.firefox.driver”, “D:\soft\Tester\driver\geckodriver.exe”);
package com.hk.sec;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.*;
import java.net.MalformedURLException;
import java.net.URL;
public class LoginTest {
private WebDriver driver;
private Map<String, Object> vars;
JavascriptExecutor js;
@Before
public void setUp() {
System.setProperty("webdriver.firefox.driver", "D:\\soft\\Tester\\driver\\geckodriver.exe");
driver = new FirefoxDriver();
js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void login() {
driver.get("http://localhost:8080/");
driver.manage().window().setSize(new Dimension(1768, 1200));
driver.findElement(By.cssSelector(".el-form-item:nth-child(1) .el-input__inner")).sendKeys("gf:gfadmin");
driver.findElement(By.cssSelector(".el-form-item:nth-child(2) .el-input__inner")).sendKeys("1");
driver.findElement(By.cssSelector(".sys-login")).click();
vars.put("vcode", driver.findElement(By.cssSelector(".el-form-item:nth-child(1) .el-input__inner")).getAttribute("value"));
vars.put("vcode", js.executeScript("var start=document.cookie.indexOf(\'vcode=\')+\'vcode=\'.length; var end=document.cookie.indexOf(\';\',start); if(end == -1) return document.cookie.substring(start); else return document.cookie.substring(start, end);"));
driver.findElement(By.cssSelector(".el-form-item:nth-child(3) .el-input__inner")).sendKeys(" vars.get(\"vcode\").toString()");
driver.findElement(By.linkText("登录")).click();
}
}
运行时可能遇到异常
java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;)V
通常是因为guava版本问题
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
需要使用正确guava版本,selenium-java会引入正确的guava版本,如果其他Jar引入guava版本,需要排除它
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
例如:
sharding-jdbc-core也会引入guava,必须排除它
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>4.1.0</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>