Java 如何使用testng和selenium参数化搜索测试
我面临一个关于搜索函数参数化的问题,事实上,我需要为我的测试提供不同的关键字: 这是我的班级:Java 如何使用testng和selenium参数化搜索测试,java,selenium,testng,selenium-grid,Java,Selenium,Testng,Selenium Grid,我面临一个关于搜索函数参数化的问题,事实上,我需要为我的测试提供不同的关键字: 这是我的班级: package Distributed; import static org.junit.Assert.assertEquals; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import org.json.JSONException; import org.openqa
package Distributed;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.json.JSONException;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TSKZ63700J {
WebDriver driver ;
String BaseURL , winURL , TestURL, androidURL ;
String ApiURL = "http://frstmwarwebsrv.orsyptst.com:9000/duobject? searchString=TSK(Z63700J)(000)(Z63700JU10)(000)&filtercheck=nameSWF&p.index=0&p.size=8";
jsonobject json;
String res;
/**
* Initiate connection
*/
@BeforeTest
public void setup () throws MalformedURLException
{
TestURL = "http://frstmwarwebsrv.orsyptst.com:9000";
BaseURL = "http://10.2.128.126";
winURL = "http://10.2.128.120:5556/wd/hub";
androidURL ="http://10.2.128.120:5555/wd/hub";
DesiredCapabilities wincap = DesiredCapabilities.firefox();
wincap.setBrowserName("firefox");
wincap.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver ( new URL ( winURL ), wincap) ;
}
@Test
public void compareresults () throws IOException, JSONException {
srch ("TSK(Z63700J)(000)(Z63700JU10)(000)");
//assertEquals(jsonobject.getresults(ApiURL), "TSK(ZRM760J)(000)(ZRM760JU00)(000)");
assertEquals(jsonobject.getresults(ApiURL), res);
}
private void srch(String keyword) {
driver.get(TestURL);
WebElement input1 = driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/input[1]"));
input1.sendKeys("guest");
WebElement input2 = driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/input[2]"));
input2.sendKeys("guest");
WebElement btn = driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/button"));
btn.click();
WebElement w1 = driver.findElement(By.xpath("html/body/header/nav/div[1]/form/div/input"));
w1.sendKeys( "TSK(Z63700J)(000)(Z63700JU10)(000)");
WebElement w2 = driver.findElement(By.xpath("(//button[@type='button'])[2]"));
w2.click();
WebDriverWait wait= new WebDriverWait(driver,10 );
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("TSK(Z63700J)(000)(Z63700JU10)(000)")));
WebElement result = driver.findElement(By.linkText("TSK(Z63700J)(000)(Z63700JU10)(000)"));
// res = (result.toString()).substring(80,100);
res =result.getText();
}
private void quitTest() {
driver.quit();
}
@AfterTest
public void aftertest ()
{
quitTest();
}
}
我想做的是:
首先对搜索函数进行参数化,我想我必须在搜索函数内部的SendKeys级别执行此操作,但我不确定如何使用Testng执行此操作
其次,我希望alsi将APIURL参数化
提前感谢您的建议
我已经做了以下工作:
@Parameters({"keyword" , })
@Test
private void srch(String keyword ) {
driver.get(TestURL);
WebElement input1 = driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/input[1]"));
input1.sendKeys("guest");
WebElement input2 = driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/input[2]"));
input2.sendKeys("guest");
WebElement btn = driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/button"));
btn.click();
WebElement w1 = driver.findElement(By.xpath("html/body/header/nav/div[1]/form/div/input"));
w1.sendKeys(keyword);
WebElement w2 = driver.findElement(By.xpath("(//button[@type='button'])[2]"));
w2.click();
}
然后更改了testng.xml:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
但是,当我运行时,只考虑最后一个参数:Integration ZCRMI120 01
谢谢
ZiedTestNG参数由可以附加到方法的注释控制 Test.java
@Parameters({"test", "test1"})
public void myTest(String test, String test1) {
driver.findElement(By.id("txtSearch")).sendKeys(test);
}
@DataProvider(name = "keywords")
public static Object[][] ngKeywords() {
return new Object[][] { { "tsk" }, { "40" }, { "30" }, { "etc" } };
}
Suite.xml
<test name="My Test">
<classes>
<parameter name="test" value="searchTerm" />
<parameter name="test1" value="some other value" />
<class name="com.my.package.Test" />
</classes>
</test>
为了使它更清晰,您可以在测试中的单独类中使用@DataProvider,然后作为参数传递 MyParameters.java
@Parameters({"test", "test1"})
public void myTest(String test, String test1) {
driver.findElement(By.id("txtSearch")).sendKeys(test);
}
@DataProvider(name = "keywords")
public static Object[][] ngKeywords() {
return new Object[][] { { "tsk" }, { "40" }, { "30" }, { "etc" } };
}
然后在测试中使用数据提供程序:
@Test(dataProviderClass = MyParameters.class, dataProvider = "keywords")
private void srch(String keyword) {
someTestStuff();
}
这允许您将数据和测试分开。只有最后一个值出现的原因是所有参数的名称都是关键字。因此,最后一个参数值是为参数“关键字”更新的最后一个参数值。如果要使用所有参数,则需要以不同的方式定义它们。在您的情况下,如果您试图使用不同的数据运行相同的测试,则应该使用dataprovider。
您的APIRL是testng.xml中一个很好的参数候选者,因为它在多个测试中都是常量。感谢Navi,我已经成功地使它与testng一起工作,我只需要在testng.xml中添加线程计数和测试标记。但感谢数据提供者的选择。