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中查找更多細節

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM