zoukankan      html  css  js  c++  java
  • Springboot Selenide UI 自动化测试

    标题中的Selenide 并没有拼错,确实不是selenium

    Selenium做UI自动化,可以参考我其他的blog; Selenium做自动化最好要封装起来,否则对于元素的等待,页面的加载会使得自己很痛苦

    这里介绍的是Selenide

    什么是 Selenide ?

    Concise UI Tests with Java!  注意用的是Java语言,所以,其它语言的同学靠边。

    Selenide = UI Testing Framework powered by Selenium WebDriver

    Github 项目地址:https://github.com/codeborne/selenide

    官方网站:http://selenide.org/index.html

    0基础搭建Springboot + Selenide 做UI的自动化测试框架,

    POM.xml

    <dependencies>
            <dependency>
                <groupId>com.codeborne</groupId>
                <artifactId>selenide</artifactId>
                <version>4.7</version>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.seleniumhq.selenium</groupId>
                        <artifactId>selenium</artifactId>
                    </exclusion>
    
                    <exclusion>
                        <groupId>org.seleniumhq.selenium</groupId>
                        <artifactId>selenium-support</artifactId>
                    </exclusion>
    
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>2.53.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-chrome-driver</artifactId>
                <version>2.53.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-api</artifactId>
                <version>2.53.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-support</artifactId>
                <version>2.53.1</version>
            </dependency>
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>7.0.0-beta1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
    
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
    
            <!--
    
                    <dependency>
                        <groupId>org.seleniumhq.selenium</groupId>
                        <artifactId>selenium-chrome-driver</artifactId>
                        <version>3.7.0</version>
                    </dependency>
    
    
    
                    <dependency>
                        <groupId>org.seleniumhq.selenium</groupId>
                        <artifactId>selenium-support</artifactId>
                        <version>3.7.0</version>
                    </dependency>
    
    
                    <dependency>
                        <groupId>org.testng</groupId>
                        <artifactId>testng</artifactId>
                        <version>7.0.0-beta1</version>
                        <scope>test</scope>
                    </dependency>-->
    
        </dependencies>
    

      测试代码,如何打开百度,进行搜索点击等操作

    package com.example.demo;
    
    import com.codeborne.selenide.Configuration;
    import org.junit.FixMethodOrder;
    import org.junit.runners.MethodSorters;
    import org.openqa.selenium.By;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;
    
    
    import static com.codeborne.selenide.Condition.text;
    import static com.codeborne.selenide.Selenide.$;
    import static com.codeborne.selenide.Selenide.open;
    import static java.lang.Thread.sleep;
    
    
    @FixMethodOrder(MethodSorters.NAME_ASCENDING) //name_ascending 是如果跑整个class 会根据方法名顺序跑
    public class Test001 {
    
        @BeforeClass
        public static void setUp() {
            System.setProperty("webdriver.chrome.driver", "D:/000_Data/Tools/chromedriver.exe");
            Configuration.timeout = 6000;
            Configuration.browser = "chrome"; //设置浏览器驱动,默认路径看下图 也可以不用默认路径
            //Configuration.reportsFolder = "test-result/reports"; selenide失败回自动保存快照  这是配置快照保存目录 默认也是这个
    
        }
    
        //自动化 访问百度
        @Test(enabled=true)
        public void step00_test() throws InterruptedException {
            open("https://www.baidu.com");
            //sleep(1000);
            $(By.id("kw")).sendKeys("acxiom");
            //$(By.id("kw")).setValue("acxiom");
            $(By.id("su")).click();
            $("body").shouldHave(text("acxiom"));
            sleep(5000);
        }
    
    }
    

    1. 打开页面 
    2. $(element).doAction()
    3. $(element).check(condition)


    通过这第一个demo,感官性的谈谈它的优点:
    1、API更简洁
    2、默认使用 CSS 定位
    3、自己封装了断言
    4、错误后自动截图 (不信你运行上面的代码试试)
    5、自动关闭浏览器。

     

    仅供参考,这些是您可能需要工作的Selenide类:

    com.codeborne.selenide.Selenide [src] [javadoc]
    图书馆的核心。主要方法是open,$和$$(进口静态com.codeborne.selenide.Selenide *的可读性。):

    open(String URL)打开浏览器(如果尚未打开)并加载URL
    $(String cssSelector) - 返回表示由页面上的CSS选择器找到的第一个元素的SelenideElement类的对象。
    $(By) - 由By类的定位器返回“第一个SelenideElement”。
    $$(String cssSelector) - 返回ElementsCollection类型的对象,表示CSS选择器找到的所有元素的集合。
    $$(By) - 由By类型的定位器返回“元素集合”。
    通常,当您通过Dollar $命令获取SelenideElement对象时,您可以对其执行一些操作:

    $(byText("Sign in")).click();
    甚至同时进行多项行动:

    $(byName("password")).setValue("qwerty").pressEnter();
    或者你可以检查一些条件:

    $(".welcome-message").shouldHave(text("Welcome, user!")).
    $$当需要的元素是同一类型的元素时,“Double Dollar”命令()可能会很有用。例如,而不是:

    $(byXpath("//*[@id='search-results']//a[contains(text(),'selenide.org')]")).click();

    你可以使用更具可读性和冗长的选择:

    $$("#search-results a").findBy(text("selenide.org")).click();

    元素的大部分操作(由$and和$$commands 获取)都根据上下文内置了隐式等待。这使得在大多数情况下,通过明确处理等待加载元素的同时自动执行动态Web应用程序的测试,不会分散注意力。

    不要害羞地在Selenide课堂内寻找更适合您需求的方法。只需输入您的IDE Selenide。并在可用的IDE提案中选择所需的选项。

    这里只是一些例子:

    sleep(), refresh()和 title(),executeJavaScript(String jsCode,Object ... arguments)。

    在Selenide gitbook中查找更多细节。

    com.codeborne.selenide.SelenideElement [src] [javadoc]
    本SelenideElement类描述的页面上找到的元素。这个类的对象可以通过$命令来获取。在这个类中定义了以下有用的方法。

    内部元素搜索方法
    find(String cssSelector) / $(String cssSelector)
    find(By) / $(By)
    findAll(String cssSelector) / $$(String cssSelector)
    findAll(By) / $$(By)
    这里$和$$只是更简洁的“别名” find和findAll方法相应。

    因此,您可以逐步指定搜索路径,构建“定位链”:

    $("#header").find("#menu").findAll(".item")
    检查元素状态的方法 - 断言
    should(Condition)/ shouldBe(Condition)/shouldHave(Condition)
    shouldNot(Condition)/ shouldNotBe(Condition)/shouldNotHave(Condition)
    我们建议选择方便的别名,以便可以像普通的英语短语一样轻松地读取代码行,例如:

    $("input").should(exist);
    $("input").shouldBe(visible);
    $("input").shouldHave(exactText("Some text"));
    断言在Selenide中扮演明确的等待角色。他们等待的状态(visible,enabled,text("some text")),以满足,直到超时达到的值(Configuration.timeout即默认设置为4000毫秒)。您可以明确使用“should-methods”,以便在相应操作之前等待元素的所需状态,例如:$("#submit").shouldBe(enabled).click();

    还有一些版本的“Selenide显式等待”能够明确设置超时:

    waitUntil(条件,毫秒)
    waitWhile(条件,毫秒)
    方法 - 对元素的操作
    click()
    doubleClick()
    contextClick()
    hover()
    setValue(String) / val(String)
    pressEnter()
    pressEscape()
    pressTab()
    selectRadio(String value)
    selectOption(String)
    append(String)
    dragAndDropTo(String)
    ...
    大多数操作返回SelenideElement的对象(同一个代理元素),允许构建简洁的方法链:$("#edit").setValue("text").pressEnter();。

    获取元素状态和属性值的方法
    getValue() / val()
    data()
    attr(String)
    text() //返回“页面上的可见文本”
    innerText() //返回“DOM中元素的文本”
    getSelectedOption()
    getSelectedText()
    getSelectedValue()
    isDisplayed()//如果元素被隐藏(不可见)或元素不存在于DOM中,则返回false; 否则 - 是真的
    exists() 如果元素存在于DOM中,则返回true,否则返回false
    其他有用的方法
    uploadFromClasspath(String fileName)
    下载()
    toWebElement()
    uploadFile(文件...)
    在Selenide gitbook中查找更多细节

    com.codeborne.selenide.Condition [src] [javadoc]
    条件用在should/ shouldNot/ waitUntil/ waitWhile结构中。我们建议静态导入相应的条件以获得可读代码的所有优点:

    可见/出现//例如$(“input”)。shouldBe(可见)
    present / exist // //在DOM中等待元素存在的条件(它仍然可以隐藏)
    隐藏/消失//不(可见)
    只读//例如$(“input”)。shouldBe(只读)
    名称//例如$(“input”)。shouldHave(name(“fname”))
    值//例如$(“input”)。shouldHave(value(“John”))
    键入//例如$(“#input”)。shouldHave(type(“checkbox”))
    id //例如$(“#input”)。shouldHave(id(“myForm”))
    空//例如$(“h2”)。shouldBe(空)
    属性(名称)//例如$(“#input”)。shouldHave(attribute(“required”))
    属性(名称,值)//例如$(“#list li”)。shouldHave(attribute(“class”,“active checked”))
    cssClass(String)//例如$(“#list li”)。shouldHave(cssClass(“checked”))
    重点
    启用


    matchText(String regex)
    文本(字符串子字符串)
    exactText(String wholeText)
    textCaseSensitive(String substring)
    exactTextCaseSensitive(String wholeText)
    在Selenide gitbook中查找更多细节

    com.codeborne.selenide.Selectors [src] [javadoc]
    该类包含一些By选择器,通过文本或属性来定位元素(在标准的Selenium WebDriver API中可能会遗漏):

    byText - 按确切文本搜索元素
    withText - 通过包含文本搜索元素(子字符串)
    by(attributeName,attributeValue) - 按属性的名称和值进行搜索
    byTitle - 按属性搜索“标题”
    byValue - 按属性搜索“值”
    Xpath的
    等等
    // Examples:
    $(byText("Login")).shouldBe(visible));
    $(By.xpath("//div[text()='Login']")).shouldBe(visible); // any org.openqa.selenium.By.* selector can be used
    $(byXpath("//div[text()='Login']")).shouldBe(visible); // or any its alternative from Selectors class
    在Selenide gitbook中查找更多细节

    com.codeborne.selenide.ElementsCollection [src] [javadoc]
    这是描述页面上由定位器找到的元素集合的类。通常可以通过该$$方法获取ElementsCollection类的对象。该类包含相当有用的方法。

    断言
    应该 - 例如$$(".errors").shouldBe(empty)
    应该 - 例如$$("#mytable tbody tr").shouldHave(size(2))
    断言也扮演显式等待的角色。他们等待的状态下(例如size(2),empty,texts("a", "b", "c"))来满足,直到超时达到的值(Configuration.collectionsTimeout即默认设置为6000毫秒)。

    获取元素集合的状态和属性的方法
    尺寸()
    是空的()
    getTexts()//返回可见元素集合文本的数组,例如对于元素:<li>a</li><li hidden>b</li><li>c</li>将返回数组["a", "", "c"]
    方法 - 特定收集元素的选择器
    filterBy(Condition) - 仅返回满足条件的原始集合元素(如ElementsCollection)的集合,例如$$("#multirowTable tr").filterBy(text("Norris"))
    excludeWith(条件) - 例如$$("#multirowTable tr").excludeWith(text("Chuck"))
    get(int) - 返回第n个元素(as SelenideElement);
    findBy(Condition) - 返回SelenideElement满足条件的第一个集合元素(as )。
    在Selenide gitbook中查找更多细节

    com.codeborne.selenide.CollectionCondition [src] [javadoc]
    集合条件用于类的对象的shouldBe/ shouldHave构造中ElementsCollection。建议静态导入所需条件以实现可读代码的所有优点。

    empty // e.g. $$("#list li").shouldBe(empty)
    size(int) // e.g. $$("#list li").shouldHave(size(10))
    sizeGreaterThan(int)
    sizeGreaterThanOrEqual(int)
    sizeLessThan(int)
    sizeLessThanOrEqual(int)
    sizeNotEqual(int)
    texts(String... substrings)
    exactTexts(String... wholeTexts)
    在Selenide gitbook中查找更多细节

    com.codeborne.selenide.WebDriverRunner [src] [javadoc]
    这个类定义了一些浏览器管理方法:

    isChrome()
    isFirefox()
    isHeadless()
    url() - 返回当前URL
    source() - 返回当前页面的源HTML代码
    getWebDriver() - 返回WebDriver实例(由Selenide自动创建或由用户设置),从而在需要时向用户提供原始API给Selenium
    setWebDriver(WebDriver) - 告诉Selenide使用由用户创建的驱动程序。从此时起,用户自己负责关闭驾驶员(例如通过呼叫getWebDriver().quit())。
    在Selenide gitbook中查找更多细节

     

  • 相关阅读:
    四轴无人机原理
    python中同步、多线程、异步IO、多线程对IO密集型的影响
    匿名四轴上位机使用手册
    python异步编程--回调模型(selectors模块)
    Python多线程异步任务队列
    jenkins配置邮箱服务器(126邮箱)
    We wanted {"required":["value"]} and you sent ["text","value","id","sessionId"]
    Updates were rejected because the remote contains work that you do
    Git更新代码
    Git 从github克隆文件至本地
  • 原文地址:https://www.cnblogs.com/qianjinyan/p/10791940.html
Copyright © 2011-2022 走看看