• Selenide使用笔记


    Selenide = UI Testing Framework powered by Selenium WebDriver

    地址:https://github.com/codeborne
    UI自动化测试框架
    介绍:
    Selenide is a framework for writing easy-to-read and easy-to-maintain automated tests in Java. It defines concise fluent API, natural language assertions and does some magic for ajax-bases applications to let you focus entirely on the business logic of your tests.

    使用:
    maven或者下载jar

    常用api,都是简单的英文,不翻译

    1. Create a browser
    Selenium WebDriver:

    DesiredCapabilities desiredCapabilities = DesiredCapabilities.htmlUnit();
    desiredCapabilities.setCapability(HtmlUnitDriver.INVALIDSELECTIONERROR, true);
    desiredCapabilities.setCapability(HtmlUnitDriver.INVALIDXPATHERROR, false);
    desiredCapabilities.setJavascriptEnabled(true);
    WebDriver driver = new HtmlUnitDriver(desiredCapabilities);
    Selenide:

    open("/my-application/login");
    // And run tests with option -Dbrowser=htmlunit (or "chrome" or "ie", default value is "firefox")
    启动浏览器的方式变化:
    如何启动chrome或者ie?
    启动chrome
    Configuration.browser = "chrome";
    System.setProperty("webdriver.chrome.driver", Constants.GETCHROMEDRIVERPATH_STRING);
    这里2选1,如果使用configuration的话,则需要在环境变量中提前配置好路径,ie如下
    System.setProperty("selenide.browser", "Chrome");
    启动IE
    Configuration.browser ="ie";
    System.setProperty("webdriver.ie.driver", Constants.GETIEIVERPATH_STRING);
    System.setProperty("selenide.browser", "Ie");
    因为我可能后续还会使用webdriver,所以这个对象还是需要的,使用driver = getWebDriver();混合使用
    2. Shutdown a browser

    Selenium WebDriver:

    if (driver != null) {
    driver.close();
    }
    Selenide:

    // Do not care! Selenide closes the browser automatically.
    With Selenide You don't need to operate with Selenium WebDriver directly. WebDriver will be automatically created/deleted during test start/finished.
    不关心浏览器的关闭,自动关闭,还有一种情况就是当执行失败的时候,我们不希望关闭浏览器,可以在configuration中配置
    3. Find element by id

    Selenium WebDriver:

    WebElement customer = driver.findElement(By.id("customerContainer"));
    Selenide:

    WebElement customer = $("#customerContainer");
    or a longer conservative option:

    WebElement customer = $(By.id("customerContainer"));

    这里的使用方法更简单,就跟我们在chrome 中console控制台调试一样,没有坑,但是补充一些没有提到到,
    补充1:
    如下:我们可能会使用xpath的定位方式处理的,这里更方便
    WebElement customer = $(byText("Customer profile"));
    WebElement temp = $(byValue("不使用")).click();
    WebElement temp1 = $(byAttribute("data-name",“test name”)).click();

    补充2:获取多个元素:
    webdriver:

    driver.findElements(By.tagName("li")).get(5);
    selenide:

    $("li", 5);
    $$("#multirowTable tr").filterBy(text("Norris"))
    上面第一行代码是所有标签为li的元素中的第5个
    第二行代码是取特定的集合后再去搜寻text为期望值的元素。
    注:
    $开头的是取一个元素
    $$开头的是取一个集合
    selenide默认支持css selector,所以代码看上去简洁了很多。

    4. Assert that element has a correct text

    Selenium WebDriver:

    assertEquals("Customer profile", driver.findElement(By.id("customerContainer")).getText());
    Selenide:

    $("#customerContainer").shouldHave(text("Customer profile"));
    elenide提供一系列should标签帮做断言工作,而且有一批text()的这种选择器来帮助我们断言各种类型。上面的例子就是断言控件是否有期望的text。selenide专门有一个condition包,里面有各种各样的condition,这些condition就是should标签的参数。上面的例子text就是一个condition。其他的还有id,value,attribute,readonly等等
    5. Ajax support (waiting for some event to happen)

    Selenium WebDriver (OMG!):

    FluentWait<By> fluentWait = new FluentWait<By>(By.tagName("TEXTAREA"));
    fluentWait.pollingEvery(100, TimeUnit.MILLISECONDS);
    fluentWait.withTimeout(1000, TimeUnit.MILLISECONDS);
    fluentWait.until(new Predicate<By>() {
    public boolean apply(By by) {
    try {
    return browser.findElement(by).isDisplayed();
    } catch (NoSuchElementException ex) {
    return false;
    }
    }
    });
    assertEquals("John", browser.findElement(By.tagName("TEXTAREA")).getAttribute("value"));
    Selenide:

    $("TEXTAREA").shouldHave(value("John"));
    This command automatically waits until element gets visible AND gets expected value.
    Default timeout is 4 seconds and it's configurable.
    可以看到selenide还是一个should 的api搞定了。 它默认4s超时。4s内会循环check控件的value是否变成了期望值。同样的还有text,attribute等选择器。
    6. Assert that element has a correct CSS class

    Selenium WebDriver:

    assertTrue(driver.findElement(By.id("customerContainer")).getAttribute("class").indexOf("errorField") > -1);
    Selenide:

    $("#customerContainer").shouldHave(cssClass("errorField"));
    如上说的,selenide默认支持css selector,
    7. Find element by text

    Selenium WebDriver:

    No way (except XPath)

    Selenide:

    WebElement customer = $(byText("Customer profile"));
    上面已经提到此处
    8. Assert that element text matches a regular expression

    Selenium WebDriver:

    WebElement element = driver.findElement(By.id("customerContainer"));
    assertTrue(Pattern.compile(".*profile.*", DOTALL).matcher(element.getText()).matches());
    Selenide:

    $("#customerContainer").should(matchText("profile"));
    9. Assert that element does not exist

    Selenium WebDriver:

    try {
    WebElement element = driver.findElement(By.id("customerContainer"));
    fail("Element should not exist: " + element);
    }
    catch (WebDriverException itsOk) {}
    Selenide:

    $("#customerContainer").shouldNot(exist);
    自带的断言,可以参考api
    10. Looking for element inside parent element

    Selenium WebDriver:

    WebElement parent = driver.findElement(By.id("customerContainer"));
    WebElement element = parent.findElement(By.className("user_name"));
    Selenide:

    $("#customerContainer").find(".user_name");
    类似Selenimu的层级定位
    11. Looking for Nth element

    Selenium WebDriver:

    WebElement element = driver.findElements(By.tagName("li")).get(5);
    Selenide:

    $("li", 5);
    上面已经提到
    12. Click "Ok" in alert dialog

    Selenium WebDriver:

    try {
    Alert alert = checkAlertMessage(expectedConfirmationText);
    alert.accept();
    } catch (UnsupportedOperationException alertIsNotSupportedInHtmlUnit) {
    return;
    }
    Thread.sleep(200); // sometimes it will fail
    Selenide:

    confirm("Are you sure to delete your profile?");
    or

    dismiss("Are you sure to delete your profile?");
    一些弹出框的操作,但是现在的前段框架很少用了,可了解
    13. Debugging info for elements

    Selenium WebDriver:

    WebElement element = driver.findElement(By.id("customerContainer"));
    System.out.println("tag: " + element.getTag());
    System.out.println("text: " + element.getText());
    System.out.println("id: " + element.getAttribute("id"));
    System.out.println("name: " + element.getAttribute("name"));
    System.out.println("class: " + element.getAttribute("class"));
    System.out.println("value: " + element.getAttribute("value"));
    System.out.println("visible: " + element.isDisplayed());
    // etc.
    Selenide:

    System.out.println($("#customerContainer"));
    // output looks like this: "<option value=livemail.ru checked=true selected:true>@livemail.ru</option>"

    打印一些元素信息,
    14. Take a screenshot

    Selenium WebDriver:

    if (driver instanceof TakesScreenshot) {
    File scrFile = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.FILE);
    File targetFile = new File("c: emp" + fileName + ".png");
    FileUtils.copyFile(scrFile, targetFile);
    }
    Selenide:


    takeScreenShot("my-test-case");
    For JUnit users it's even more simpler:

    public class MyTest {
    @Rule // automatically takes screenshot of every failed test
    public ScreenShooter makeScreenshotOnFailure = ScreenShooter.failedTests();
    }

    截图控件,默认的保存路径可以通过configuration配置或者同时保存源代码
    15. Select a radio button

    Selenium WebDriver:

    for (WebElement radio : driver.findElements(By.name("sex"))) {
    if ("woman".equals(radio.getAttribute("value"))) {
    radio.click();
    }
    }
    throw new NoSuchElementException("'sex' radio field has no value 'woman'");
    Selenide:

    selectRadio(By.name("sex"), "woman");
    单选操作
    16. Reload current page

    Selenium WebDriver:

    webdriver.navigate().to(webdriver.getCurrentUrl());
    Selenide:

    refresh();
    17. Get the current page URL, title or source

    Selenium WebDriver:

    webdriver.getCurrentUrl();
    webdriver.getTitle();
    webdriver.getPageSource();
    Selenide:

    url();
    title();
    source();

  • 相关阅读:
    final有什么用?
    数组的定义
    作业
    List 、Set数据结构
    报表工具实现单据套打
    动态格报表的制作
    图形钻取
    报表工具轻松搞定卡片式报表
    列表钻取
    报表中如何实现不规则布局
  • 原文地址:https://www.cnblogs.com/hylinux/p/6383337.html
Copyright © 2020-2023  润新知