2017-04-18 66 views
1

我試圖自動化一個方案,其中,我想登錄一次到應用程序&然後進行操作,而無需再次重新登錄。在TestNG中運行多個類

想一想,我有代碼在特定類的@BeforeSuite方法中登錄到應用程序中。

public class TestNGClass1 { 

    public static WebDriver driver; 

    @BeforeSuite 
    public static void setUp(){ 
     System.setProperty("webdriver.chrome.driver", "D://Softwares//chromedriver.exe"); 
     driver = new ChromeDriver(); 
     //driver = new FirefoxDriver(); 
     driver.manage().window().maximize(); 
     driver.get("https://www.myfitnesspal.com"); 
    } 

    @AfterSuite 
    public static void close(){ 
     driver.close(); 
    } 
} 

我有我的@test方法在TestNGClass2基本上試圖點擊一些登錄按鈕。

public class TestNGClass2 extends TestNGClass1 { 

    public static WebDriver driver; 

    @Test 
    public static void login(){ 
     System.out.println("Entering the searchQuery Box"); 
     WebElement signUpWithEmailBtn = driver.findElement(By.xpath(".//*[@id='join']/a[2]")); 
     System.out.println("srchTxtBox Box"); 
     signUpWithEmailBtn.click(); 
    } 
} 

我有另一個TestNGClass3類,它有另一個@Test方法,需要在TestNGClass2完成後運行。

public class TestNGClass3 extends TestNGClass1{ 

public static WebDriver driver; 

    @Test 
    public static void signIn(){ 
     WebElement emailAddress = driver.findElement(By.id("user_email")); 
     emailAddress.clear(); 
     emailAddress.sendKeys("[email protected]"); 
     WebElement password = driver.findElement(By.id("user_password")); 
     password.clear(); 
     password.sendKeys("sdass"); 

     WebElement continueBtn = driver.findElement(By.id("submit")); 
     continueBtn.click(); 
    } 
} 

的testng.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="Suite"> 
    <test name="Regression Test"> 
     <classes> 
      <class name="com.test.TestNGClass2" /> 
      <class name="com.test.TestNGClass3" /> 
     </classes> 
    </test> <!-- Test --> 
</suite> <!-- Suite --> 

是我的做法正確,因爲當代碼達到TestNGClass2的「登錄」方法我得到「空指針」異常?

回答

4

我想你只需要在擺脫這一行的都你TestNGClass2TestNGClass3

public static WebDriver driver; 

你已經存儲driver在基類TestNGClass1,所以當你有一個線在你的其他類,你基本上隱藏了實例化的類。

我還考慮將基類訪問修飾符更改爲protected,因爲您可能不希望該類基類的子類不能訪問driver

+0

是的可能! – kushal

+0

@mrfreester:感謝一堆,它工作:) – AdiBoy