设置selenium webdriver的默认执行速度 [英] Set selenium webdriver's default execution speed

查看:1866
本文介绍了设置selenium webdriver的默认执行速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm running some GUI tests with webdriver. I exported some tests directly from selenium IDE. In this test I had to decrease IDE's speed to run it due to the loading of a drop down. How can I slow down my test in Selenium webdriver? I already put

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

并且它保持快速运行。我知道睡眠选项,但这不是我想要的,我想改变webdriver的默认执行速度。这是我的代码:

and it kept running at a fast speed. I know the sleep option but that's not what I'm looking for, I would like to change the webdriver's default execution speed. Here's my code:

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;
    }
}
}

阅读一些答案,没有人帮助

Read some answers and none helped

减少Selenium Webdriver的速度

https://sqa.stackexchange.com/questions/8451/how-can-i-reduce-the-执行速度在webdriver-so-that-c​​an-view-proper-wh

Selenium IDE - 将默认速度设置为慢速

Selenium IDE - Set default speed to slow

推荐答案

不要使用 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);
}

这篇关于设置selenium webdriver的默认执行速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆