我正在使用Selenium,Java,Cucumber构建自动化项目。
此时我有3个不同的类来运行自动化代码 - Container Class:将所有WebElements存储为单个方法 - 查看类:所有逻辑都在这里。 exp:对存储在容器类中的Webelement执行单击。 -Steps Class:我断言或验证场景的不同部分
我的问题是,当我从我的视图类调用容器方法时,我没有在网络上找到这些元素...
我知道路径没问题,因为如果我在步骤类中构建整个场景,所有WebElements都没有问题
我知道视图类正在激活容器类,因为我在容器方法中设置了一个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));
}
答案 0 :(得分:0)
尽量减少代码。最好创建一个公共类,您可以在其中放置登录,设置和拆卸功能,并使用主类(包含主逻辑和调用常见所需的功能)。其次,尝试照顾访问修饰符。第三,尝试检查可以定义XPATH的不同类型。希望它会有所帮助。
答案 1 :(得分:0)
Try to use this : instead of a retry
public WebElement findElementSafe(By selector, long timeOutInSeconds) {
try {
return findElement(selector, timeOutInSeconds);
} catch (TimeoutException e) {
return null;
}
}
public WebElement findElement(By selector, long timeOutInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.presenceOfElementLocated(selector));
return findElement(driver, selector);
}
public void waitForElementToAppear(By selector, long timeOutInSeconds) {
WebDriverWait wait = new WebDriverWait(getDriver(), timeOutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(selector));
}
public void waitForElementToDisappear(By selector, long timeOutInSeconds) {
WebDriverWait wait = new WebDriverWait(getDriver(), timeOutInSeconds);
wait.until(ExpectedConditions.invisibilityOfElementLocated(selector));
}