我无法在@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" }
};
}
}
答案 0 :(得分:3)
在@BeforeTest public void setup(){}
内,您可以创建
ChromeDriver driver = new ChromeDriver();
在testMethod
中使用的是外部定义的那个:
WebDriver driver;
更改
ChromeDriver driver = new ChromeDriver();
带
driver = new ChromeDriver();