【发布时间】:2016-02-12 22:23:08
【问题描述】:
我正在使用 Selenium、Java、Cucumber 构建一个自动化项目。
此时我有 3 个不同的类在运行自动化代码 - 容器类:将所有 WebElement 存储为单个方法 - 查看类:所有逻辑发生的地方。 exp:对存储在容器类中的 Webelement 执行点击。 -Steps 类:我在其中断言或验证场景的不同部分
我的问题是当我从视图类调用容器方法时,我没有在网页端找到 webelements...
我知道路径没问题,因为如果我在步骤类中构建整个场景,所有 WebElement 都可以毫无问题地定位
我知道视图类正在执行容器类,因为我在容器方法中设置了一个 prinln,并且每次视图类尝试查找 WebElement 时都会显示。
容器类
package com.automation.automation.prototype.containers;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class loginContainer {
private WebDriver driver;
public loginContainer(WebDriver driver) {
this.driver = driver;
}
public WebElement loginEmail(){
//xpath that is not beign located
return driver.findElement(By.xpath("//*[@id='email']"));
}
}
查看类
package com.automation.automation.prototype.views;
import com.automation.automation.prototype.containers.loginContainer;
import org.openqa.selenium.WebDriver;
import com.automation.automation.prototype.containers.loginContainer;
public class loginView {
private static loginContainer login;
public loginView(WebDriver driver) {
login = new loginContainer(driver);
}
public boolean insertEmail( String email) throws InterruptedException{
boolean valid = false;
int flag = 0;
int attemps = 0;
do{
attemps++;
try{
login.loginEmail().click();
login.loginEmail().sendKeys(email);
System.out.println("Element Found!: "+attemps);
valid = true;
flag = 1;
}
catch(Exception e){
Thread.sleep(1000);
System.out.println("Element Searching: "+attemps);
}
}while (attemps <20 && flag ==0);
return valid;}
}
步类
package com.automation.automation.prototype;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import static org.junit.Assert.assertTrue;
import com.automation.automation.prototype.containers.URLs;
import com.automation.automation.prototype.containers.loginContainer;
import com.automation.automation.prototype.views.loginView;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class steps {
private WebDriver driver;
private static URLs URL;
private static loginView LoginView2;
//private static loginContainer LoginContainerstep;
public steps(){
URL = new URLs(driver);
//LoginContainerstep = new loginContainer(driver);
LoginView2 = new loginView(driver);
}
@Given(value = "^Launch$")
public void login() throws Throwable{
driver = new FirefoxDriver();
driver.get(URL.fbURL);
System.out.println(URL.fbURL);
}
@Then(value = "^insertCredentials (.*)$")
public void insertCredentials_and_and(String email) throws InterruptedException{
assertTrue("ELEMENT IS NOT SHOWING UP", LoginView2.insertEmail(email));
}
【问题讨论】:
-
您面临什么异常。提供那个
-
你真的应该尽量减少代码以重现失败+提供异常
-
我真的很好奇为什么你在构造函数中创建
LoginView2 = new loginView(driver),但你在Given方法中创建driver = new FirefoxDriver()。您应该在调用之前创建驱动程序。 -
提供异常堆栈跟踪。
标签: java object selenium automated-tests