![](/img/trans.png)
[英]What is the difference between driver.switchTo().parentFrame() and driver.switchTo().defaultContent() method in Selenium WebDriver?
[英]What the difference in Initializing selenium driver?
我使用黄瓜+硒。 我创建了Auth测试,它看起来像这样
public class AuthTestSteps {
private static WebDriver driver;
private static WebDriverWait wait;
@Given("^some condition$")
public void some_condition(){
System.setProperty("webdriver.chrome.driver","chromedriver.exe");
driver = new ChromeDriver();
wait = new WebDriverWait(driver, 30);
}
@If("^open link \\\"(.*)\\\"$")
public void open_link(String pageUrl){
driver.get(pageUrl);
wait.until(ExpectedConditions.visibilityOfElementLocated(Auth.profileBtn));
Auth.clickProfileBtn(driver);
}
.....
该测试效果很好,可以满足我的所有需求,但是我尝试创建另一个测试。 很简单:
public class CallDoctorTestStep {
private static WebDriver driver;
private static WebDriverWait wait;
@Given("^some condition$")
public void нужно_вызвать_врача(){
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
wait = new WebDriverWait(driver, 30);
}
@IF("^some condition \\\"(.*)\\\"$")
public void пользователь_перейдет_на_главную(String pageUrl){
driver = new ChromeDriver();
driver.get(pageUrl);
wait.until(ExpectedConditions.visibilityOfElementLocated(CallDoctor.callDoctorBtn));
CallDoctor.clickCallDoctor(driver);
}
...........
如您所见,差异很小,但是! 当我尝试启动CallDocrotTestStep
时, If
条件为null,则失败。 浏览器无法启动。 所以我试图搜索,什么问题。 还有初始化问题,如果我将driver
初始化为CallDoctorTestStep类字段,例如: private static WebDriver driver = new ChromeDriver();
它将启动浏览器。 我认为变量的可见性存在问题,但是为什么AuthTestStep
可以很好地工作,但是如果我像AuthTestStep
那样初始化driver
, CallDoctorTestStep
就不能工作?
您已经声明
private static WebDriver driver;
private static WebDriverWait wait;
在AuthTestSteps类中。
访问修饰符private
表示只能在声明它们的类(在本例中为AuthTestSteps)内部访问它们。 这意味着类CallDoctorTestStep无法访问它们,如您所说。
要在您的StepDefinition之间共享状态,请看一下使用依赖注入(DI)。 Cucumber支持多个DI框架,您可以在此处阅读有关它们的更多信息: https : //cucumber.io/docs/reference/java-di
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.