selenium-webdriver - 使用 TestNG 运行时无法解析驱动程序
问题描述
package bannerTstNG;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class BannerTestNG {
@BeforeTest
public void OpenTheSuperAdmin() throws InterruptedException {
System.setProperty("webdriver.chrome.driver","D:\\myselenium\\bannerTstNG\\driver\\chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://ss-superadmin-staging.labaiik.net/");
driver.manage().window().maximize();
driver.findElement(By.xpath("//input[@id='email']")).sendKeys("arsalan.hameed@avrioc.com");
driver.findElement(By.xpath("//input[@id='password']")).sendKeys("admin");
Thread.sleep(1000);
driver.findElement(By.xpath("//button[contains(text(),'Login')]")).click();
Thread.sleep(6000);
}
@Test
public void ClickOnBanner() {
driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/div[1]/div[3]/div[1]/ul[1]/li[4]/a[1]")).click
}
}
该函数OpenTheSuperAdmin()
正在运行,但在ClickOnBanner()
执行时,出现以下错误:driver cannot be resolved.
为什么OpenTheSuperAdmin()
执行没有任何错误并且驱动程序错误没有显示在那里?
解决方案
您正在方法内部实例化driver
变量,OpenTheSuperAdmin
因此它超出了ClickOnBanner
测试范围。请尝试以下方法:
package bannerTstNG;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class BannerTestNG {
private WebDriver driver;
@BeforeTest
public void OpenTheSuperAdmin() throws InterruptedException {
System.setProperty("webdriver.chrome.driver","D:\\myselenium\\bannerTstNG\\driver\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://ss-superadmin-staging.labaiik.net/");
driver.manage().window().maximize();
driver.findElement(By.xpath("//input[@id='email']")).sendKeys("arsalan.hameed@avrioc.com");
driver.findElement(By.xpath("//input[@id='password']")).sendKeys("admin");
Thread.sleep(1000);
driver.findElement(By.xpath("//button[contains(text(),'Login')]")).click();
Thread.sleep(6000);
}
@Test
public void ClickOnBanner() {
driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/div[1]/div[3]/div[1]/ul[1]/li[4]/a[1]")).click
}
}
通过将驱动程序声明为类中的一个字段,您现在可以从类内的任何测试中访问它。
推荐阅读
- geocoding - 计算用户之间的距离是 Geocoding API 的一项功能吗?
- sql - 在 where 子句中使用列值作为变量名 [已解决]
- python - MySQLdb._exceptions.OperationalError OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
- python - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x95 in position 3: invalid start byte (Python) socket programming
- c - 如何在不使用 chdir() 函数的情况下在我自己的 C 语言 shell 中实现 cd 命令?
- python - 我无法获得执行命令的按钮。(tkinter)
- python - 安装python依赖项时Docker容器构建错误
- javascript - JS输入onClick未定义
- virtual-machine - 虚拟化特定环境(CPU、缓存、时钟)
- node.js - 如何使用nodejs读取内存中的targz文件夹