无法通过Bitbucket流水线运行时使用Selenium定位元素。

huangapple go评论78阅读模式
英文:

Unable to locate elements via Selenium if ran via Bitbucket pipeline

问题

我目前正在Selenium中进行集成测试。我有一些测试集,用于测试网页元素的存在和它们的行为。如果我在本地机器上运行这些测试,一切都运行得很完美。作为WebDriver,我使用ChromeDriver。

但是,如果我在Bitbucket的流水线上运行其中一些测试,其中一些测试会失败。它失败的原因是无法定位元素,最糟糕的是,在流水线中它的行为有点不确定 - 有时它能够定位到这个元素,有时则定位不到(在找到元素的运行和未找到元素的运行之间没有更改任何代码,被测试的网站在运行之间也没有更改。)

> 预期的条件失败:等待定位元素的出现...(尝试了300秒,间隔为500毫秒)

在我的本地机器上,即使它不是非常快,30秒的超时对我来说完全足够。并且在我本地的机器上测试始终通过。是不是Bitbucket上的流水线运行非常慢,还是我漏掉了什么?

webDriver.get(getTerminalUrl());
WebDriverWait wait = new WebDriverWait(webDriver, 60);

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[text()='Sign In']")));
WebElement login = webDriver.findElement(By.xpath("//*[text()='Sign In']"));
login.click();

我在所有的测试中都使用这种模式。

英文:

I am currently working on integration tests in Selenium. I have some testset, which tests presence of webelements and their behaviour. Everything works perfectly if I run these tests on my local machine. As a WebDriver I use ChromeDriver.

But some of these tests fail, if I run them in pipeline on bitbucket. It fails because it was not able to locate element, the worst thing is that in pipeline it behaves sort of undeterministic - that it sometimes locate this element and sometimes it doesn't (no code changed between run in which element was found and in which element was not found, Website tested was not changed either between runs.)

> Expected condition failed: waiting for presence of element located by... (tried for 300 second(s) with 500 milliseconds interval)

On my local machine, which is not really fast one, 30s timeout is perfectly enough for me. And tests always pass on my local machine. Is it that pipeline on bitbucket runs extremely slowly or do I miss something?

webDriver.get(getTerminalUrl());
WebDriverWait wait = new WebDriverWait(webDriver, 60);

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[text()='Sign In']")));
WebElement login = webDriver.findElement(By.xpath("//*[text()='Sign In']"));
login.click();

I use this kind of pattern in all my tests.

答案1

得分: 0

请尝试这样做:

webDriver.get(getTerminalUrl());
WebDriverWait wait = new WebDriverWait(webDriver, 60);

boolean elementPresent = driver.findElements(By.xpath("//*[text()='Sign In']")).size() > 0;

if (elementPresent) {
    Thread.sleep(3000);
    WebElement login = webDriver.findElement(By.xpath("//*[text()='Sign In']"));
    Thread.sleep(3000);
    login.click();
}
英文:

Try doing like this:

webDriver.get(getTerminalUrl());
WebDriverWait wait = new WebDriverWait(webDriver, 60);

boolean elementPresent = driver.findElements(By.xpath("//*[text()='Sign In']")).size() > 0;

if(elementPresent){
    Thread.sleep(3000);
    WebElement login = webDriver.findElement(By.xpath("//*[text()='Sign In']"));
    Thread.sleep(3000);
    login.click();
}

huangapple
  • 本文由 发表于 2020年8月27日 16:32:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63612158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定