在Base类中,我以static WebDriver driver = new FirefoxDriver();
那么我是否需要在第二节中包含相同的static WebDriver driver = new FirefoxDriver();
?
1个案例:我在第二个类中包含static WebDriver driver = new FirefoxDriver();
但是,在执行第二个类时未执行。我在我的班级中也使用了@BeforeMethod
和@AfterMethod
TestNG注释。但是,它对我不起作用。如果我在这里做错了什么,请帮助我。
在下面的例子中,我想继续我的第二课的测试执行,我停止了我的第一堂课:
例如,在我登录到应用程序的第一个类中,登录后我现在在主页上。所以现在我想进一步从主页开始执行我的第二课。但是,在执行第一个类并登录到应用程序后,第二个类不执行。
在“运行套件的结果”选项卡中执行测试套件后,显示以下针对类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文件:
<?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();
}
}
答案 0 :(得分:1)
根据提供的示例,您在每个类中启动了webdriver并在该类中退出。如果您在两个类中使用相同的webdriver,那么最好在一个单独的类中启动webdriver,然后将此类扩展到您的测试用例类(除了您的基础和第二类之外)。
让我们看一个简单的例子,我将创建一个类配置,我启动我的webdriver
public class config{
static WebDriver driver;
@BeforeSuite
public void setup(){
driver=new FirefoxDriver();
}
@AfterSuite
public void tearDown(){
driver.quit();
}
}
现在,我将把这个类扩展到我的测试用例/类
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 =&#34; true&#34;按指定顺序执行测试用例/类
<?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 -->
谢谢你, 穆拉利