我想准备一个参数化的硒测试。我用Selenium Firefox IDE研究了参数化。这没有问题。它使用JS文件等。但是当我将它作为Java代码导出为Junit时,从js文件获取值作为参数不起作用。
以下是selenium HTML和导出的Java代码;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://www.google.com.tr/" />
<title>Parameterization_Example</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">New Test</td></tr>
</thead><tbody>
<tr>
<td>open</td>
<td>/</td>
<td></td>
</tr>
<tr>
<td>storeEval</td>
<td>email</td>
<td>email</td>
</tr>
<tr>
<td>storeEval</td>
<td>password</td>
<td>password</td>
</tr>
<tr>
<td>clickAndWait</td>
<td>id=gbi4t</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>id=Email</td>
<td>${email}</td>
</tr>
<tr>
<td>type</td>
<td>id=Passwd</td>
<td>${password}</td>
</tr>
<tr>
<td>clickAndWait</td>
<td>id=signIn</td>
<td></td>
</tr>
</tbody></table>
</body>
</html>
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import com.thoughtworks.selenium.SeleneseTestCase;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.Select;
public class SeleniumTest extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "/home/ahmet/Desktop/chromedriver");
WebDriver driver = new ChromeDriver();
String baseUrl = "http://www.google.com.tr";
selenium = new WebDriverBackedSelenium(driver, baseUrl);
}
@Test
public void testU() throws Exception {
selenium.open("/");
String email = selenium.getEval("email");
String password = selenium.getEval("password");
selenium.click("id=gbi4t");
selenium.waitForPageToLoad("30000");
selenium.type("id=Email", email);
selenium.type("id=Passwd", password);
selenium.click("id=signIn");
selenium.waitForPageToLoad("30000");
}
@After
public void tearDown() throws Exception {
selenium.stop();
}
}
当它尝试使用getEval
函数获取值时,会发生SeleniumException
。
有没有办法为Selenium Firefox IDE直接生成的Java代码提供参数化?
注意:代码会更新一些,只是为了使用ChromeDriver。
答案 0 :(得分:0)
使用以下代码创建SelectItem.java文件。
public class SelectItem {
private String label;
private String value;
public SelectItem(String label, String value) {
this.label = label;
this.value = value;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
然后创建SeleniumTest.java文件并在@Test部分添加以下代码。
@Test
public void testU() throws Exception
{
java.util.List<SelectItem> selectItems = new java.util.ArrayList<SelectItem>();
selectItems.add(new SelectItem("Email1","Password1"));
selectItems.add(new SelectItem("Email2","Password2"));
for(SelectItem selItem:selectItems)
{
String user = selItem.getLabel();
String password = selItem.getValue();
selenium.click("id=gbi4t");
selenium.type("id=Email", email);
selenium.type("id=Passwd", password);
selenium.click("id=signIn");
}
}