即使在执行@BeforeTest Annotation后,也无法转到@Test Annotation

时间:2017-11-10 10:42:59

标签: selenium annotations

我无法在@test中执行代码,但它在@BeforeTest Annotation中执行。它打开了在BeforeTest Annotation中的驱动程序,但它无法找到@中的元素(" // * [@ id =' lst-ib']")测试并抛出NULL异常。

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" }
                };

    }

}

1 个答案:

答案 0 :(得分:3)

@BeforeTest public void setup(){}内,您可以创建

的实例
ChromeDriver driver = new ChromeDriver();

testMethod中使用的是外部定义的那个:

WebDriver driver;

更改

    ChromeDriver driver = new ChromeDriver();

    driver = new ChromeDriver();