Java Selenium TestNG测试取决于其他方法
先决条件 大多数网站的流量如下所示: 其中“dashboard”是所有有趣的特定于站点的业务逻辑发生的地方 有什么问题吗? 我正在尝试使用SeleniumWebDriver和TestNG来测试这样一个站点。到目前为止,我的代码库大致如下: TestNG.xml MercuryLogin.javaJava Selenium TestNG测试取决于其他方法,java,selenium,testng,Java,Selenium,Testng,先决条件 大多数网站的流量如下所示: 其中“dashboard”是所有有趣的特定于站点的业务逻辑发生的地方 有什么问题吗? 我正在尝试使用SeleniumWebDriver和TestNG来测试这样一个站点。到目前为止,我的代码库大致如下: TestNG.xml MercuryLogin.java test()。没有发生登录,即使类调用它如何确保MercuryToursTestLoginFirst实际登录并使用数据提供程序?我猜您使用的是教程网站上的代码? 我想你误解了@beforeTest的概念
test()。没有发生登录,即使类调用它如何确保MercuryToursTestLoginFirst
实际登录并使用数据提供程序?我猜您使用的是教程网站上的代码?
我想你误解了@beforeTest的概念
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@BeforeTest:annotated方法将在属于标记内类的任何测试方法运行之前运行。
您不能在1中组合这两个标记(@Test+@BeforeTest)。这样做毫无意义。您告诉Testng在其他测试之前运行此方法@BeforeTest通常用于配置,就像使用driver.exe一样。将@Test仅用于测试目的
那么现在我们想用@BeforeTest实现什么呢?也许我误解了。我只是想在对仪表板执行任何测试之前自动让Selenium WebDriver登录。既然您提到它正在工作,请删除该部分。但如果您真的希望在Mercury类中添加另一个@BeforeTest,那么它应该是另一个方法。最佳实践,将其放在任何@测试之前。订单->1。在2号套房之前。在测试3之前。在方法4之前。测试结果表明,我的问题的尾部不仅不清楚,而且危险性接近于XY问题。我要重构它。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<!-- Root tag for TestNG.xml will always be suite. The name can be whatever you want -->
<suite name="MySmokeTestSuite">
<test name="MyFirstTest">
<groups>
<run>
<exclude name="regression"></exclude>
<include name="smoke"></include>
</run>
</groups>
<classes>
<class name="com.mikewarren.testsuites.MercuryLogin">
<methods>
<include name="methodName"></include>
<!-- you can also include or exclude methods -->
</methods>
</class>
<!-- You can have the class tag for multiple classes of unique name -->
</classes>
</test>
</suite>
package com.mikewarren.testsuites;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
public class MercuryToursTest {
protected static WebDriver driver;
protected String url = "http://newtours.demoaut.com";
@BeforeTest
public void beforeTest()
{
System.setProperty("webdriver.chrome.driver", "Drivers/chromedriver.exe" );
driver = new ChromeDriver();
driver.get(url);
// wait a second
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
}
@AfterTest
public void afterTest()
{
if (driver != null)
driver.quit();
}
}
package com.mikewarren.testsuites;
import java.io.File;
import java.io.FileInputStream;
import java.util.concurrent.TimeUnit;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.mikewarren.pages.MercuryLoginFactory;
public class MercuryLogin extends MercuryToursTest {
@Test(priority=0, groups={"smoke"})
public void validateLandingPage() {
Assert.assertEquals(driver.getTitle(), "Welcome: Mercury Tours");
}
@Test(dependsOnMethods="validateLandingPage",
priority=2,
groups={"regression", "somethingElse"},
dataProvider="provideAccountDetailsDynamic")
// @BeforeTest(groups = {"loginFirst"})
public void loginToMercury(String username, String password)
{
MercuryLoginFactory mlf = new MercuryLoginFactory(driver);
mlf.driverLogIntoMercury(username, password);
driver.findElement(By.xpath("//a[contains(text(), 'Home')]")).click();
}
@DataProvider
public Object[][] provideAccountDetailsDynamic() throws Exception {
File file = new File("src/test/resources/mercuryData.xlsx");
FileInputStream fis = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheet("sheet1");
int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
Object[][] data = new Object[rowCount][2];
/*
* Data driven framework example.
* • This is a design patern for test automation where you develop the tests in a manner where they will run
* based on provided data. In this case, a tester could have 3 rows data, warranting the test to run 3
* separate times with the given values.
* This allows for configurable automation tests at the hands of a non-developer.
*/
for (int i = 1; i <= rowCount; i++)
{
Row row = sheet.getRow(i);
data[i-1] = new Object[] {
row.getCell(0).getStringCellValue(),
row.getCell(1).getStringCellValue()
};
}
return data;
}
}
package com.mikewarren.testsuites;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class MercuryToursTestLoginFirst extends MercuryToursTest {
@BeforeClass(dependsOnGroups = "loginFirst")
public void init()
{
}
@Test
public void test()
{
System.out.println("mock test");
}
}
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.