简体   繁体   English

用于在不同环境中测试的javascript中的selenium-webdriver的策略

[英]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)
{

}
  • Basically I am putting a wait for the document to be ready using JavaScript executor 基本上我正在等待使用JavaScript执行器准备好文档
  • Also I am putting an implicit wait for each element that I would access. 此外,我正在隐藏等待我将访问的每个元素。 I am using expected condition as ElementToBeClickable because sometime ElementIsPresent does not mean element can be accessed as per your needs. 我将期望的条件用作ElementToBeClickable,因为有时ElementIsPresent并不意味着可以根据您的需要访问元素。

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.

相关问题 selenium-webdriver - JavaScript, findElement a href, &lt;&gt; 不是 function - selenium-webdriver - JavaScript, findElement a href, <> is not a function 测试React组件时,在selenium-webdriver中解决StaleElementReference - Working around StaleElementReference in selenium-webdriver when testing a react component 如何在 Selenium (selenium-webdriver) JavaScript 中使用 Chrome 配置文件 - How to use Chrome profile in Selenium (selenium-webdriver) JavaScript selenium-webdriver:如何在javascript中使用driver.quit() - selenium-webdriver: how to use driver.quit() in javascript Selenium-WebDriver 如何使用 javascript 和 firefox 浏览器突出显示元素 - Selenium-WebDriver how to highlight element using javascript and firefox browser selenium-webdriver 在 javascript 中找不到驱动程序? - selenium-webdriver can't find the driver in javascript? Selenium-webdriver / Javascript:如何滚动页面? - Selenium-webdriver/Javascript: How do I scroll through a page? 在节点javascript中使用selenium-webdriver执行“命令” - executing “commands” using selenium-webdriver in node javascript 如何使用带有JavaScript的Selenium-WebDriver选择所有复选框 - How to select all checkboxes with Selenium-WebDriver with javascript 如何通过 selenium-webdriver javascript API 设置“debuggerAddress”chromeOption? - How to set "debuggerAddress" chromeOption via selenium-webdriver javascript API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM