2017-11-10 8 views
2

@testの中でコードを実行することはできませんが、@BeforeTest注釈で実行されます。 BeforeTestアノテーションにあるドライバが開かれていますが、@Testにある要素( "// * [@ id = 'lst-ib']」)を見つけることができず、NULL例外がスローされます。あなたの@BeforeTest public void setup(){}testMethodに外に定義されたものを使用している間、あなたが@BeforeTest Annotationを実行した後でも@Test Annotationに移動できません

ChromeDriver driver = new ChromeDriver(); 

のインスタンスを作成インサイド

package package2; 

public class dataprovider { 
    WebDriver driver; 

    @BeforeTest 
    public void setup(){ 

     System.setProperty("webdriver.chrome.driver","./drivers/chromedriver.exe/"); 
     ChromeDriver driver = new ChromeDriver(); 
     driver.manage().window().maximize(); 
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
     driver.get("https://google.com/"); 
     /*System.setProperty("webdriver.firefox.marionette", "./drivers/geckodriver.exe/"); 
      driver = new FirefoxDriver(); 
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
      driver.get("https://google.com");*/ 
    } 

    /** Test case to verify google search box 
    * @param author 
    * @param searchKey 
    * @throws InterruptedException 
    */ 

    @Test(dataProvider="SearchProvider") 
    public void testMethod(String author,String searchKey) throws InterruptedException{ 
     { 

      WebElement searchText = driver.findElement(By.xpath("//*[@id='lst-ib']")); 
      //search value in google searchbox 
      searchText.sendKeys(searchKey); 
      System.out.println("Welcome ->"+author+" Your search key is->"+searchKey); 
      Thread.sleep(3000); 
      String testValue = searchText.getAttribute("value"); 
      System.out.println(testValue +"::::"+searchKey); 
      searchText.clear(); 
      //Verify if the value in google search box is correct 
      Assert.assertTrue(testValue.equalsIgnoreCase(searchKey)); 
     } 
    } 
    /** 
    * @return Object[][] where first column contains 'author' 
    * and second column contains 'searchKey' 
    */ 

    @DataProvider(name="SearchProvider") 
    public Object[][] getDataFromDataprovider(){ 
     return new Object[][] 
       { 
      { "Guru99", "India" }, 
      { "Krishna", "UK" }, 
      { "Bhupesh", "USA" } 
       }; 

    } 

} 

答えて

3

WebDriver driver; 

を変更

ChromeDriver driver = new ChromeDriver(); 

driver = new ChromeDriver(); 
+0

その作業、ありがとう、 –

+0

@bhargavporapuこの回答または他の誰かがあなたの問題を解決した場合は、それを受け入れ済みとしてマークしてください:stackoverflow.com/help/someone-answers –

関連する問題