无法从 Testng.xml 文件执行第二个类 [英] not able to execute second Class from Testng.xml file
问题描述
在基类中,我启动浏览器为 static WebDriver driver = new FirefoxDriver();
In the Base class I initiate browser as static WebDriver driver = new FirefoxDriver();
那么我是否需要在第二个类中包含相同的 static WebDriver driver = new FirefoxDriver();
?
Then do I need to include same static WebDriver driver = new FirefoxDriver();
in the second class ?
1 案例:我在第二个类中也包含了 static WebDriver driver = new FirefoxDriver();
但是,在执行期间第二个类没有执行.我在我的两个类中也使用了 @BeforeMethod
和 @AfterMethod
TestNG 注释.但是,它对我不起作用.如果我在这里做错了什么,请帮助我.
1 Case: Where I included static WebDriver driver = new FirefoxDriver();
in the second class as well however, during the execution second class in not executed. I used @BeforeMethod
and @AfterMethod
TestNG annotation as well in my both class. However, it is not working out for me. Please help me if I am doing anything wrong here.
在下面的例子中,我想从我停止第一类测试的地方继续我的第二类测试执行:
In the below case I want to continue my test execution with second class from where I stopped my first Class test:
例如,在第一堂课中,我登录了应用程序,登录后我现在在主页上.所以现在我想从主页开始进一步执行我的第二节课.但是,在执行第一类并登录应用程序后,第二类没有执行.
For example in the first class I logged into application and after login I am on Home page now. So now I want to further execute my second class from Home page onward. However, after executing first class and login into application, second class is not executing.
在运行套件的结果"选项卡中执行测试套件后显示以下类别 2 的错误消息:
After Test suite execution in the "Result of running suite" tab show following error message for class 2:
java.lang.NullPointerException
at com.proweb.Web2.navigation(ProviderInformation.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.access$000(SuiteRunner.java:37)
at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:368)
at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
这是我的 testNG XML 文件:
Here is my testNG XML file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="2" parallel="tests" >
<test name="AllTest">
<classes>
<class name="com.proweb.web1" />
<class name="com.proweb.web2" />
</classes>
</test>
</suite>
这是我的基类:
package com.proweb;
import java.io.File;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Web1 {
public WebDriver driver;
Workbook wb;
Sheet sh1;
int numrow;
String username;
String password;
@BeforeMethod
public void oneTimeSetUp() {
static WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://example.com");
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id='app']/div/main/section/ul/li[1]/a")).click();
Thread.sleep(1000);
}
@Test(dataProvider="testdata")
public void testFireFox(String uname,String password1) throws InterruptedException
{
driver.findElement(By.xpath("//input[@name='username']")).clear();
Thread.sleep(1000);
driver.findElement(By.xpath("//input[@name='username']")).sendKeys(uname);
Thread.sleep(1000);
driver.findElement(By.xpath("//input[@name='password']")).clear();
Thread.sleep(1000);
driver.findElement(By.xpath("//input[@name='password']")).sendKeys(password1);
Thread.sleep(1000);
driver.findElement(By.xpath("//button[@name='loginButton']")).click();
Thread.sleep(1000);
}
@DataProvider(name="testdata")
public Object[][] TestDataFeed(){
try {
// load workbook
wb=Workbook.getWorkbook(new File("C://File//Book2.xls"));
// load sheet in my case I am referring to first sheet only
sh1= wb.getSheet(0);
// get number of rows so that we can run loop based on this
numrow= sh1.getRows();
}
catch (Exception e)
{
e.printStackTrace();
}
// Create 2 D array and pass row and columns
Object [][] logindata=new Object[numrow][sh1.getColumns()];
// This will run a loop and each iteration it will fetch new row
for(int i=0;i<numrow;i++){
// Fetch first row username
logindata[i][0]=sh1.getCell(0,i).getContents();
// Fetch first row password
logindata[i][1]=sh1.getCell(1,i).getContents();
}
// Return 2d array object so that test script can use the same
return logindata;
}
@AfterMethod
public void afterMethod() {
// Close the driver
driver.quit();
}
}
这是我的第二堂课:
package com.proweb;
import java.io.File;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Web2 {
public WebDriver driver;
Workbook wb;
Sheet sh2;
int numrow;
String firstname;
String middlename;
String lastname;
@BeforeMethod
public void SetUp() {
static WebDriver driver = new FirefoxDriver();
driver.findElement(By.xpath("//*[@id='main-navigation']/span[1]")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id='app']/div/header/nav[1]/div/ul/li/ul/li[2]/a")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id='app-main']/main/div/div[1]/div/a/span[2]")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id='-selector_input']")).sendKeys("xyz123xyz");
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id='selector_listbox__option__0']")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id='app-main']/main/div/div[3]/button")).click();
Thread.sleep(3000);
}
@Test(dataProvider="testdata")
public void testProvidername(String fname,String mname,String lname) throws InterruptedException
{
driver.findElement(By.xpath("//*[@id='nameFirst']")).clear();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id='nameFirst']")).sendKeys(fname);
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id='nameMiddle']")).clear();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id='nameMiddle']")).sendKeys(mname);
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id='nameLast']")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id='nameLast']")).sendKeys(lname);
Thread.sleep(1000);
}
@DataProvider(name="testdata")
public Object[][] TestDataFeed(){
try {
// load workbook
wb=Workbook.getWorkbook(new File("C://File//Book2.xls"));
// load sheet in my case I am referring to first sheet only
sh2= wb.getSheet(1);
// get number of rows so that we can run loop based on this
numrow= sh2.getRows();
}
catch (Exception e)
{
e.printStackTrace();
}
// Create 2 D array and pass row and columns
Object [][] Peinformationdata=new Object[numrow][sh2.getColumns()];
// This will run a loop and each iteration it will fetch new row
for(int i=0;i<numrow;i++){
// Fetch first row username
Peinformationdata[i][0]=sh2.getCell(0,i).getContents();
// Fetch first row password
Peinformationdata[i][1]=sh2.getCell(1,i).getContents();
Peinformationdata[i][2]=sh2.getCell(2,i).getContents();
}
// Return 2d array object so that test script can use the same
return Peinformationdata;
}
@AfterMethod
public void afterMethod() {
// Close the driver
driver.quit();
}
}
推荐答案
根据提供的示例,您在每个类中启动了 webdriver 并在该类中退出了相同的.如果您在两个类中使用相同的 webdriver,那么最好在一个单独的类中启动 webdriver,然后将该类扩展到您的测试用例类(除了您的基类和第二类).
As per provided example, you initiated webdriver in each class and quitting the same in that class. If you what to use same webdriver in two classes, then its good to initiate webdriver in one separate class and then extends this class to your test cases classes(nothing but yours base and second class).
让我们看一个简单的例子,我将创建一个类配置来启动我的网络驱动程序
let see one simple example, i will create a class config where i initiate my webdriver
public class config{
static WebDriver driver;
@BeforeSuite
public void setup(){
driver=new FirefoxDriver();
}
@AfterSuite
public void tearDown(){
driver.quit();
}
}
现在,我将把这个类扩展到我的测试用例/类
Now, i will extend this class to my test cases/classes
public class NewTest1 extends config{
@Test
public void test1() {
driver.get("http://www.google.com");
}
}
换个班
public class NewTest2 extends config{
@Test
public void MyTesting() {
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
}
现在,从tesng.xml中执行这两个类,注意使用preserve-order="true"按照指定的顺序执行测试用例/类
Now, execute these two classes from tesng.xml please note to use preserve-order="true" to execute test cases/classes as specified order
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false" preserve-order="true">
<test name="TestA">
<classes>
<class name="com.test.NewTest1"/>
<class name="com.test.NewTest2"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
谢谢,壁画
这篇关于无法从 Testng.xml 文件执行第二个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!