2012-07-06 57 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"); 
} 
}