[英]Strategy for selenium-webdriver in javascript for testing in different environments
I've been making a functional tests using selenium-webdriver using yadda library. 我一直在使用yadda库使用selenium-webdriver进行功能测试。 The problem it's that in my different environments the same test suite working different.
问题在于,在我的不同环境中,相同的测试套件工作方式不同。 Example:
例:
On tests, the result it's different based on the environment that I entry. 在测试中,结果根据我输入的环境而不同。
Local localhost:5000 本地 localhost:5000
Open my search site
․ when i go to my site: 2169ms
․ when i write a text on the search input: 21ms
․ when i click the search button: 130ms
․ then i get the results page.": 46ms
Staging mystaging.domain.com 暂存 mystaging.domain.com
Open my search site:
StaleElementReferenceError: {"errorMessage":"Element is no longer attached to the DOM","request":{"headers":{"Accept":"application/json; charset=utf-8","Connection":"close","Content-Length":"2"
Production www.domain.com 制作 www.domain.com
Open my search site
․ when i go to my site: 2169ms
․ when i write a text on the search input: 21ms
․ when i click the search button: 130ms
․ then i get the results page.": 46ms
At this time, only the staging tests are failing, but in other situations when the internet connection it's slow, the tests fails in production but pass in staging. 此时,只有分段测试失败,但在其他情况下,当互联网连接速度很慢时,测试在生产中失败但传递分段。 The main problem it's that the browser doesn't have the DOM ready for the test and they doesn't find the element required for the test.
主要的问题是浏览器没有为测试准备好DOM,他们没有找到测试所需的元素。
My approach for trying to solve this, it's wait that appear the root element of my page like this: 我试图解决这个问题的方法,等待出现在我的页面的根元素,如下所示:
return driver.wait(() => driver.isElementPresent(By.css(".my__homepage")), 50000);
But this isn't enough for me, because the test suites are still failing randomly.So, my question it's: 但这对我来说还不够,因为测试套件仍然随机失败。所以,我的问题是:
Which could be a best approach for run in different environments the tests suite dealing with the elements that isn't ready on the browser ? 这可能是在不同环境中运行的最佳方法,测试套件处理浏览器上尚未准备好的元素?
I am showing you a simple C# code which I did for my work in IE browser but it can work similarly for other browsers and languages. 我向您展示了一个简单的C#代码,我在IE浏览器中为我的工作做了这些代码,但它可以用于其他浏览器和语言。
private static WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(120)); //max driver wait timeout 120 seconds
try
{
//waiting for document to be ready
IJavaScriptExecutor jsExecutor = (IJavaScriptExecutor)driver;
wait.Until(sc => jsExecutor.ExecuteScript("return document.readyState").Equals("complete"));
//waiting for an element.
//use 'ExpectedConditions.ElementToBeClickable' as in some cases element gets loaded but might not be still ready to be clicked
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("elementID")));
}
catch (Exception ex)
{
}
Note: Addiotinally you might also have to check for other element properties (like enabled/disabled, etc) depending on your needs. 注意:Addiotinally您可能还需要检查其他元素属性(如启用/禁用等),具体取决于您的需要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.