【问题标题】:Set selenium webdriver's default execution speed设置 selenium webdriver 的默认执行速度
【发布时间】:2015-08-18 08:29:23
【问题描述】:

我正在使用 webdriver 运行一些 GUI 测试。我直接从 selenium IDE 导出了一些测试。在这个测试中,由于加载了一个下拉菜单,我不得不降低 IDE 的运行速度。如何减慢 Selenium webdriver 中的测试速度?我已经放了

 driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

它一直在以极快的速度运行。我知道 sleep 选项,但这不是我想要的,我想更改 webdriver 的默认执行速度。这是我的代码:

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ProfileCheck {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private final StringBuffer verificationErrors = new StringBuffer();

@Before
public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://localhost:8080";
    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
}

@Test
public void testProfileCheck() throws Exception {
    System.out.println("Test if profiles have access to the screen");
    // Administrateur (has access)

    driver.get(baseUrl + "/Dashboard/index.jsp?lang=en");
    driver.findElement(By.cssSelector("input[name=combo_profile]")).click();
    driver.findElement(
            By.cssSelector(".x-boundlist-item:contains('Administrateur')"))
            .click();
    driver.get(baseUrl + "/Params/ClientCutOff/index.jsp?lang=en");
    try {
        assertTrue(driver.getTitle().matches("^[\\s\\S]*ClientCutOff$"));
    } catch (Error e) {
        verificationErrors.append(e.toString());
    }
    // Habilitation (no access)
    driver.get(baseUrl + "/Dashboard/index.jsp?lang=en");
    driver.findElement(By.cssSelector("input[name=combo_profile]")).click();
    driver.findElement(
            By.cssSelector(".x-boundlist-item:contains('Habilitation')"))
            .click();
    driver.get(baseUrl + "/Params/ClientCutOff/index.jsp?lang=en");
    try {
        assertFalse(driver.getTitle().matches("^[\\s\\S]*ClientCutOff$"));
    } catch (Error e) {
        verificationErrors.append(e.toString());
    }
}

@After
public void tearDown() throws Exception {

    try {
        Thread.sleep(5000);
        driver.quit();
    } catch (Exception e) {
    }
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
        fail(verificationErrorString);
    }
}

private boolean isElementPresent(By by) {
    try {
        driver.findElement(by);
        return true;
    } catch (NoSuchElementException e) {
        return false;
    }
}

private boolean isAlertPresent() {
    try {
        driver.switchTo().alert();
        return true;
    } catch (NoAlertPresentException e) {
        return false;
    }
}

private String closeAlertAndGetItsText() {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        if (acceptNextAlert) {
            alert.accept();
        } else {
            alert.dismiss();
        }
        return alertText;
    } finally {
        acceptNextAlert = true;
    }
}
}

阅读了一些答案,但没有任何帮助

Decreasing the speed of Selenium Webdriver

https://sqa.stackexchange.com/questions/8451/how-can-i-reduce-the-execution-speed-in-webdriver-so-that-i-can-view-properly-wh

Selenium IDE - Set default speed to slow

【问题讨论】:

  • 是的,正如其他人所说,不要依赖定时等待。而是使用等待直到调用 - wait(until.elementLocated(By.css('csss selector')) 或 .wait(until.titleIs('title for the page''), 1000); 这样您还可以测试更多您的代码 - 出现正确的页面了吗?(css 选择器)元素出现了吗?

标签: java user-interface selenium selenium-webdriver selenium-ide


【解决方案1】:

不要使用sleep!!!

public static WebElement findElement(WebDriver driver, By selector, long timeOutInSeconds) {
    WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
    wait.until(ExpectedConditions.presenceOfElementLocated(selector));

    return findElement(driver, selector);
}

【讨论】:

  • 同意这一点。使用等待,它会更好,并且可以保护您的测试速度。
【解决方案2】:

曾经有一个“setSpeed()”方法用于 Selenium WebDriver 的 Java 绑定。但它已被弃用,因为它对浏览器自动化毫无意义。

相反,如果驱动程序比加载您的元素或类似的东西“更快”,您应该始终智能地使用等待来处理这些情况。

总而言之,目前在 WebDriver 本身内没有故意减慢执行速度的选项。除了实现 Thread.sleep() 之外,目前没有其他方法可以显式减慢您的步骤

但您可能会探索其他一些选项:

例如,如果您想减慢点击元素的速度,您可以编写自己的方法来点击元素:

public void clickElement(WebElement element) {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    element.click();
}

如果您想减慢 findElement 方法的速度,您可以编写另一个辅助方法:

public void findElement(By by) {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    driver.findElement(by);
}

您甚至可以从 WebDriver 类之一编写自己的扩展类,如 here 所述,如下所示:

public class MyFirefoxDriver extends FirefoxDriver {

@Override
public WebElement findElement(By by) {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return by.findElement((SearchContext) this);
}

您可以为所有想要减慢速度的执行编写这些方法。

【讨论】:

  • 除了它很有意义。现在有这么多高级(即过度设计的)网站不是为浏览器自动化的快速操作而设计的,而且会崩溃。为每个单独的操作检查所有可能的等待元素是令人发狂的乏味。他们不应该删除该功能。但是 Selenium 从来没有遇到过除了极其基本的用户之外的任何人真正遇到过的问题,这就是为什么 Watir 是一个东西。
【解决方案3】:

在正常情况下,当它寻找一个元素时,Selenium 会一直尝试寻找一个元素,直到你设置了“隐式等待”的值为止。这意味着如果比这更早找到元素,它将继续执行测试。我不是 Java 专家,但在您提供的代码中,我无法识别任何正在寻找下拉项目的位。由于下拉菜单可以在加载其中的实际项目之前很久就加载,我敢打赌这就是你的问题:它正在寻找下拉菜单本身,找到它,停止等待并开始尝试执行其余的测试失败,因为项目尚未加载。因此,解决方案是寻找您尝试选择的特定项目。不幸的是,我对 Java 不够精通,无法为您提供实际的代码解决方案。

顺便说一句,根据我的经验,当从 Selenium IDE 导出到其他东西时,您最终会得到几乎但不完全与您期望的测试完全不同的测试。他们可能做你期望他们做的事,但他们通常会走捷径,或者至少与你自己编写测试代码不同的方法。

【讨论】:

  • 我尝试在 Selenium IDE 中进行测试,并在有问题的部分加载之前设置了一个目标为 3000 的暂停,它加载并且它工作。然后令我惊讶的是,当我导出代码时,没有任何停顿。我还使用目标 3000 制作了命令 setSpeed 并且效果很好,但是当我导出时,我在评论中得到了它
  • 是的,这部分是我的观点。根据我的经验,从长远来看(甚至在“中等”运行中)学习如何使用可以直接与 Selenium 交互的编程或脚本语言来构建测试要比使用转换更有效从 IDE 中会产生令人困惑的行为。
猜你喜欢
  • 2011-09-23
  • 2013-07-06
  • 2012-01-24
  • 1970-01-01
  • 1970-01-01
  • 2017-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多