你好,我作为注册用户不熟悉StackOverflow,实际上我已经为学习目的而与数据提供者一起编写了一些代码。但是,当通过testNg.xml执行它时,第一个测试将结果显示为
的两倍 package com.qa.zoho.rough;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestCase1 {
WebDriver driver;
@Test(dataProvider="getdata")
public void doLogin(String username, String password,String browser) throws InterruptedException {
if(browser.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\src\\test\\resources\\executables\\chromedriver.exe");
driver= new ChromeDriver();
}
else if (browser.equals("firefox")){
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"\\src\\test\\resources\\executables\\geckodriver.exe");
driver= new FirefoxDriver();
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.zoho.com");
driver.findElement(By.xpath("//a[@class='zh-login']")).click();
driver.findElement(By.id("lid")).sendKeys(username);
driver.findElement(By.id("pwd")).sendKeys(password);
driver.findElement(By.id("signin_submit")).click();
Thread.sleep(3000);
driver.quit();
}
@DataProvider
public Object[][] getdata(){
Object[][] data= new Object[2][3];
data[0][0]="tester1sel@yahoo.com";
data[0][1]="Confidential";
data[0][2]="Confidential";
data[1][0]="tester2sel@yahoo.com";
data[1][1]="Confidential";
data[1][2]="firefox";
return data;
}
}
package com.qa.zoho.rough;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestCase2 {
WebDriver driver;
@Test(dataProvider="getdata")
public void doLogin(String username, String password,String browser) throws InterruptedException {
if(browser.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\src\\test\\resources\\executables\\chromedriver.exe");
driver= new ChromeDriver();
}
else if (browser.equals("firefox")){
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"\\src\\test\\resources\\executables\\geckodriver.exe");
driver= new FirefoxDriver();
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.zoho.com");
driver.findElement(By.xpath("//a[@class='zh-login']")).click();
driver.findElement(By.id("lid")).sendKeys(username);
driver.findElement(By.id("pwd")).sendKeys(password);
driver.findElement(By.id("signin_submit")).click();
Thread.sleep(3000);
driver.quit();
}
@DataProvider
public Object[][] getdata(){
Object[][] data= new Object[1][3];
data[0][0]="kasif135@yahoo.com";
data[0][1]="Confidential";
data[0][2]="chrome";
return data;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="TestChrome">
<classes>
<class name="com.qa.zoho.rough.TestCase1"/>
</classes>
</test>
<test name="TestFirefox">
<classes>
<class name="com.qa.zoho.rough.TestCase2"/>
</classes>
</test>
</suite>
=================套房结果======================
我不知道发生了什么,我尝试更新testng依赖关系,也更新了testNG库,但仍然无法解决此问题,即使我在testNG.xml中交换了测试顺序,然后是第二个测试结果显示两次。
答案 0 :(得分:0)
更改测试用例1中的getData行:
Object[][] data= new Object[1][3];
data[0][0]="kasif135@yahoo.com";
data[0][1]="Confidential";
data[0][2]="chrome";
return data;
这样,您将只释放1个对象,而不是2个。