【发布时间】:2019-03-12 18:01:08
【问题描述】:
请给我合理的解决方案。我已经应用了 ImplicitWait 并且它忽略了等待时间并使脚本失败。我应该怎么做才能让它工作。加载该网站大约需要 10-15 秒,我将等待时间设置为 45 秒。它等待大约 3-4 秒并引发错误。
这是我的框架的 BaseClass 代码:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.ABC.PageObjects.LoginPage;
import com.ABC.Utilities.ReadConfig;
public class BaseClass {
ReadConfig readconfig = new ReadConfig();
public String baseUrl = readconfig.getApplicationUrl();
public String baseUrl1 = readconfig.getApplicationUrl1();
public String username = readconfig.getUsername();
public String password = readconfig.getPassword();
public static WebDriver driver;
public static Logger log;
@Parameters("browser")
@BeforeClass
public void SetUp(String br) throws IOException {
log = Logger.getLogger("ABC");
PropertyConfigurator.configure("log4j.properties");
if(br.equals("chrome")) {
System.setProperty("webdriver.chrome.driver",readconfig.getChropath());
driver = new ChromeDriver();
}
else if(br.equals("firefox")) {
System.setProperty("webdriver.gecko.driver",readconfig.getFirefoxpath());
driver = new FirefoxDriver();
}
else if(br.equals("ie")) {
//System.setProperty("webdriver.gecko.driver",readconfig.getFirefoxpath());
driver = new InternetExplorerDriver();
}
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);// its waiting only 3-4 sec
driver.manage().window().maximize();
driver.get(baseUrl);
}
@AfterClass(enabled = true)
public void TearDown() {
driver.quit();
}
public void captureScreen(WebDriver driver, String tname) throws IOException {
TakesScreenshot ts = (TakesScreenshot) driver;
File source = ts.getScreenshotAs(OutputType.FILE);
File target = new File(System.getProperty("user.dir") + "/Screenshots/" + tname + ".png");
FileUtils.copyFile(source, target);
System.out.println("Screenshot taken");
}
这是隐式等待不起作用的测试用例
public class TC_001_LoginTest extends BaseClass {
@Test
public void Login() throws IOException {
log.info("url is opened");
LoginPage lp = new LoginPage(driver);
lp.setUserName(username);
log.info("Username entered");
lp.setPassword(password);
log.info("Password entered");
lp.clickSubmit();
log.info("clicked");`
有人知道怎么回事吗?
这是我的页面对象类
public class LoginPage {
WebDriver ldriver;
public LoginPage(WebDriver rdriver)
{
ldriver=rdriver;
PageFactory.initElements(rdriver, this);
}
@FindBy(name="username")
@CacheLookup
WebElement txtUserName;
@FindBy(id="password")
@CacheLookup
WebElement txtPassword;
@FindBy(id="btnLogin")
@CacheLookup
WebElement btnLogin;
public void setUserName(String uname)
{
txtUserName.sendKeys(uname);
}
public void setPassword(String pwd)
{
txtPassword.sendKeys(pwd);
}
public void clickSubmit()
{
btnLogin.click();
}
【问题讨论】:
-
您的代码暗示您正在使用页面对象并且可能使用页面工厂。也请与您的原始帖子分享相应的代码。当您使用页面工厂初始化页面对象时,它有自己的隐式等待参数。
-
感谢比尔的回复。是的,我正在使用页面对象。我更新了我的原始帖子并添加了页面对象类。感谢您的帮助
-
你能比“抛出错误”更具体吗?发布您的堆栈跟踪。此外,您可能将驱动程序参数传递给 initElements 而不是本地副本。对页面对象的后续调用可能会失败。
-
这里是什么意思:org.openqa.selenium.WebDriverException:未知错误:元素 不可点击 ------------------------- -------------------------------------------------- --------------------------所以它输入用户名和密码然后点击提交。我想在登录后执行几个动作。所以登录大约需要 10 秒,但它会在 2-3 秒后抛出该错误消息。隐式等待根本不起作用
-
我对不同的项目使用了完全相同的框架,并且隐式等待工作正常。但在这种情况下它根本不起作用