2012-07-06 67 views
2

我想准备一份硒参数测试。我已经研究了有关Selenium Firefox IDE的参数化。这没有问题。它使用一个JS文件等。但是当我将它作为Java代码导出为Junit时,从js文件获取值作为参数不起作用。Selenium Firefox IDE to JAVA code as parametrizated

这里是硒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

使用以下代码创建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"); 
} 
}