Selenium+Java+Chrome進行web自動化實例(初學者)


1.准備工作

第一步  安裝JDK

第二步 下載Eclipse

第三步 在Eclipse中安裝TestNG

第四步 下載Chrome、chromedriver.exe注意這兩個要版本對應,可以搜Chrome與ChromeDriver版本對照表

ChromeDriver 鏡像 http://npm.taobao.org/mirrors/chromedriver

2.開始Test

在Eclipse中配置Maven,新建Maven項目,添加依賴

  (1)修改Maven的配置文件。下面這兩處需要修改

<localRepository>D:\repository</localRepository>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> </settings>

 

(2)pom添加依賴
<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.14.2</version> <scope>test</scope> </dependency> </dependencies>

這里我遇到的問題是我導入了下面的依賴之后,就一直報錯Exception in thread “main” java.lang.NoClassDefFoundError: okhttp3/ConnectionPool 

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>

selenium-java-3.9.x以上版本依賴於okhttp ,但是項目中這個jar包是存在的,不知道什么原因導致的報錯,之后換成上面低版本的selenium-java依賴就可以執行了。

 

新建測試類

(1)先簡單測試一下

package com.aia; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver;
public class Test { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "E:\\test\\chromedriver_win32\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.baidu.com/"); try { Thread.sleep(2*1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
可能會遇到ChromeDriver啟動Chrome瀏覽器后,地址欄只顯示data;搜到的解決辦法大致是這幾種

1、查看是否少了http頭部:比如要用 http://localhost:3000 而不是localhost:3000

2、chromeDriver版本不對或者太舊,嘗試下載chromeDriver版本對應的chrome瀏覽器版本

3、可以嘗試在代碼里加入配置信息:參見下面的方法 options.addArguments();

用了下面的測試類,跑出來的效果是先是打開瀏覽器的窗口顯示data;后續會再次打開瀏覽器窗口打開代碼里的連接。
 
 
         
(2)稍微多點東西,涉及定位頁面元素的方法 ,具體可以學習下面鏈接
 
         
https://www.cnblogs.com/hustar0102/p/5965095.html

package com.aia; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeDriverService; import org.openqa.selenium.chrome.ChromeOptions; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.io.File; import java.util.concurrent.TimeUnit;
public class LT_TestDemo08 { ChromeDriver driver=null; String chromeBinaryPath = "C:\\Users\\sunflower\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe"; String chromeDriverPath = "E:\\test\\chromedriver_win32\\chromedriver.exe"; @BeforeMethod public void startupDriver(){ ChromeOptions options = new ChromeOptions(); options.setBinary(chromeBinaryPath); options.addArguments(String.format("window-size=%s,%s", 1200, 900)); ChromeDriverService service = new ChromeDriverService.Builder() .usingDriverExecutable(new File(chromeDriverPath)) .usingPort(8999).build(); //.usingAnyFreePort().build(); this.driver = new ChromeDriver(service, options); this.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); this.driver.manage().deleteAllCookies(); } public void login() throws Exception{ this.driver.get("http://127.0.0.1:8092"); Thread.sleep(2*1000); WebElement webButtonLoginOnIndex = this.driver.findElement(By.xpath("//button[text()='登錄']")); webButtonLoginOnIndex.click(); Thread.sleep(2*1000); WebElement webInputUserName = this.driver.findElement(By.xpath("//input[@name='username']")); webInputUserName.sendKeys("wangdong@automation.com"); Thread.sleep(2*1000); WebElement webInputUserPwd = this.driver.findElement(By.xpath("//input[@name='password']")); webInputUserPwd.sendKeys("123"); Thread.sleep(2*1000); WebElement webButtonLogin = this.driver.findElement(By.xpath("//input[@value='登錄']")); webButtonLogin.click(); Thread.sleep(2*1000); WebElement webLinkFileUpload = this.driver.findElement(By.xpath("//*[text()='Locators']")); webLinkFileUpload.click(); Thread.sleep(2*1000); } @Test public void test08_tag() throws Exception{ login(); WebElement tagName = this.driver.findElement(By.xpath("//h4[text()='Tag Name']/..")); WebElement element = tagName.findElement(By.tagName("span")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("標簽span")); } @Test public void test08_name() throws Exception{ login(); WebElement element = this.driver.findElement(By.name("xiaolv")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("小綠")); } @Test public void test08_linkText() throws Exception{ login(); WebElement element = this.driver.findElement(By.linkText("jquery.com")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("jquery.com")); } @Test public void test08_id() throws Exception{ login(); WebElement element = this.driver.findElement(By.id("mac")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("mac")); } @Test public void test08_css() throws Exception{ login(); WebElement element = this.driver.findElement(By.cssSelector("#right_div > div > div:nth-child(6) > span.label.label-info")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("height 65px;")); } @Test public void test08_className() throws Exception{ login(); WebElement element = this.driver.findElement(By.className("classC")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("classC")); } @Test public void test08_xpath() throws Exception{ login(); WebElement element = this.driver.findElement(By.xpath("//h4[text()='Xpath']/..//*[text()='35 sec.']")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("35 sec.")); } @Test public void test08_partialLinkText() throws Exception{ login(); WebElement element = this.driver.findElement(By.partialLinkText("旅游")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("旅游指南")); } @AfterMethod public void shutdownDriver(){ if(this.driver!=null){ try{ //this.driver.close(); this.driver.quit(); }catch (Exception e) { e.printStackTrace(); } } } }

 右鍵 run as  TestNG Test ,就會看到瀏覽器自動打開,並搜索代碼的地址,完成代碼的動作,瀏覽器關閉。


免責聲明!

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



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