Selenium 學習總結(來自吳楓)


Selenium學習總結

一、 Selenium功能介紹:

框架底層使用JavaScript模擬真實用戶對瀏覽器進行操作。測試腳本執行時,瀏覽器自動按照腳本代碼做出點擊,輸入,打開,驗證等操作,就像真實用戶所做的一樣,從終端用戶的角度測試應用程序。支持錄制方式:selenium ide;及多種語言腳本,如python,ruby,java,c#,php,perl,javascript等

二、 Selenium+java主要流程:

  1. 環境准備

1) java環境(參考:http://www.cnblogs.com/iceb/p/7561752.html

a)      官網下載或直接安裝(jdk-8u111-windows-x64.exe)

http://www.oracle.com/technetwork/java/javase/downloads/index.html

b)      按照步驟,一步一步安裝

c)      配置環境變量

d)      查看版本,檢驗安裝是否成功。

2) 集成環境

a)      Eclipse(eclipse_v4.5.0.exe)

b)      Idea(推薦)

ideaIC:開源版,功能有限制(ideaIC-2017.1.4.exe)

ideaIU:商業版,需要破解(ideaIU-2017.1.exe)

按照步驟一步步安裝即可。ideaU激活地址:

http://idea.iteblog.com/key.php

http://intellij.mandroid.cn/

http://idea.imsxm.com/

3) Selenium server(selenium-server-standalone-3.9.1.jar)

a)      保存到本地即可

4) 瀏覽器插件

a)      Firefox插件:geckodriver.exe

b)      Chrome插件:chromedriver.exe

保存到本地即可

備注:firefox較新版本(48及之后),僅可支持Selenium3.0之后的版本;Selenium2.X啟動firefox不需要安裝額外的插件,Selenium3.X啟動firefox,需要安裝geckdriver.exe插件。

5) Maven:(參考:http://www.cnblogs.com/iceb/p/7097850.html

a)      下載到本地,解壓縮(apache-maven-3.3.9-bin.zip)

b)      配置環境變量

c)      查看版本,驗證是否安裝成功

d)      提換settings.xml文件(目錄:C:\Users\wufeng\.m2)

  1. 搭建框架

1) java+selenium+junit

新建java項目

 

 

  添加junit、selenium依賴

 

 

 

 

 

 

 java+Selenium+junit框架搭建完畢

 

2) java+selenium+maven+testing

新建一個maven項目

 

 

Pom.xml添加selenium及testng的依賴

 

 

 

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.9.13</version>
    </dependency>
</dependencies>

創建testng.xml配置文件

 

 

 

 

Testing.xml位置沒有影響,建議放在項目根目錄

Testng.xml文件添加:

 

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
    <test name="Test1">
        <classes>
            <class name="DemoTest"/>
        </classes>
    </test>
</suite>

Pom.xml中添加testng配置

 

 

<build>
    <!-- 添加插件 關聯testng.xml -->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>
</build>

Testing報告配置

 

 

 

 

Java+selenium+maven+testng搭建完畢

  1. 編寫腳本:

a)      初始化瀏覽器

b)      訪問鏈接

c)      獲取頁面元素

d)      元素控件操作

e)      校驗結果

f)       關閉瀏覽器

g)      調試,修改腳本

 

  1. Selenium+java
  2. Selenium元素定位

https://www.cnblogs.com/qingchunjun/p/4208159.html

 

  1. Selenium對頁面控件的操作

https://blog.csdn.net/u013998857/article/details/70313171

列表、多選、單選

https://blog.csdn.net/qiaotong1/article/details/50843662

 

  1. 調試過程:

1) 獲取頁面元素方法:如Chrome/Firefox瀏覽器,Fn鍵+F12鍵,開大開發者工具, 圖標,獲取頁面元素

2) 元素XPath信息獲取:開發者工具模式下,右鍵復制,XPath。

3) 常見控件操作:

a)      瀏覽器訪問:

driver.get("http://baidu.com");

b)      文本框錄入:

driver.findElement(By.name(“search-input”)).sendKeys(“美國”);

c)      鏈接/按鈕點擊:

driver.findElement(By.linkText(“收藏”)).click

driver.findElement(By.className(“search-btn”)).click;

d)      獲取元素上文字:

driver.findElement(By.xpath("//*[@class='tc-15-btn btn-follow2 weak attention']")).getText();

e)      獲取元素屬性值:

driver.findElement(By.id(“kw”)). getAttribute(“style”);

f)       模擬鍵盤操作:

driver.findElement(By.id("su")).sendKeys(Keys.chord(Keys.CONTROL, "a"));

 

  1. 實例-百度搜索:

import org.junit.Assert;

import org.junit.Test;

import org.openqa.selenium.By;

import org.openqa.selenium.NoSuchElementException;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

 

public class TestSearch {

 

    @Test

    public void testSearch() throws InterruptedException {

 

        //初始化

        System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");

        WebDriver driver = new FirefoxDriver();

        driver.manage().window().maximize();

 

        //模擬頁面操作

        String query = "selenium";

 

        driver.get("http://baidu.com");

        driver.findElement(By.id("kw")).sendKeys(query);

        driver.findElement(By.id("su")).click();

 

        Thread.sleep(1000);

 

        //結果校驗

        boolean status;

        try {

            driver.findElement(By.xpath("//*[contains(.,'" + query + "')]"));

            System.out.println(query + ",查詢到結果");

            status = true;

        } catch (NoSuchElementException e) {

            status = false;

            System.out.println(query + ",未查詢到結果");

        }

        Assert.assertTrue(status);

 

        //瀏覽器關閉

        driver.quit();

    }

}


免責聲明!

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



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