Fork me on GitHub

Selenium 学习笔记

Selenium 学习笔记

WebDriver

What:

  1. each of browser has its own browser driver
  2. each has an API maintained by the browser vendor not the selenium
  3. Drivers inplemented in same language as browser
  4. Wrapper around browser driver (封装好的,不用懂内部构造)

Goal why

  1. Quickly and easily write automated test
  2. Maintain a standardized API that is user friendly
  3. Emulate actions of users

Who:

  1. to test across multiple browsers and platforms
  2. a customizable and powerful framework

How

  1. using client server communication
  2. a new session of browser is created, browser window is lanuched
  3. for each request of Test script, a request is sent to a Web Driver API
  4. The webDriver API interpreters the request and execute in the browser
  5. when each step completed, responses are sent to the API and the test script

WorkFlow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;

public class TestSample {
public static void main(String[] args) {

// Set the property for webdriver.chrome.driver to be the location to your local download of chromedriver
System.setProperty("webdriver.chrome.driver", "/Users/meaghanlewis/Downloads/chromedriver");

// Create new instance of ChromeDriver
WebDriver driver = new ChromeDriver();

// And now use this to visit Google
driver.get("http://www.google.com");

// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));

// Enter something to search for
element.sendKeys("Cheese!");

// Now submit the form
element.submit();

//Close the browser
driver.quit();
}
}

在代码中 new ChromeDriver() 时,selenium会随机挑选一个端口调用chromedriver程序,调用成功后 chromedriver 会在指定的端口启动一个服务(会有一个进程)

1
2
3
> >tasklist | find "chromedriver"
> chromedriver.exe 7848 Console 1 13,740 K
>

selenium 中使用 apache 的 commons-exec 来运行 chromedriver.exe 启动 ChromeDriver 服务
直接在控制台运行 chromedriver.exe 时,默认端口是9515

selenium 通过指定的端口和约定的协议来和 ChromeDriver 进行通信,一个ChromeDriver可用管理多个chrome。

1
2
3
4
5
6
7
8
9
> >tasklist | find "chrome"
> chromedriver.exe 14692 Console 1 14,888 K
> chrome.exe 20952 Console 1 72,204 K
> chrome.exe 7288 Console 1 9,052 K
> chrome.exe 15524 Console 1 10,000 K
> chrome.exe 13036 Console 1 96,028 K
> chrome.exe 11836 Console 1 29,836 K
> chrome.exe 2788 Console 1 59,876 K
>

selenium 中多个 WebDriver 实例对应一个 chromedriver 进程,一个 chromedriver 进程管理多个 chrome 进程。
一个 WebDriver 实例对应一个浏览器窗口。

作者:冬瓜baba
链接:https://www.jianshu.com/p/31c8c9de8fcd
来源:简书

接受各种自动化测试的网站

http://formy-project.herokuapp.com/

各种测试网站元素

  1. Text and button
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class KeyboardAndMouseInput {
public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "/Users/shuolintian/Downloads/chromedriver");

WebDriver driver = new ChromeDriver();

driver.get("https://formy-project.herokuapp.com/keypress");

WebElement name = driver.findElement(By.id("name"));
name.click();
name.sendKeys("shoadfadf");

WebElement button = driver.findElement(By.id("button"));
button.click();

driver.quit();
}
}
  1. autocomplete
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Autocomplete {
public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "/Users/shuolintian/Downloads/chromedriver");

WebDriver driver = new ChromeDriver();

driver.get("https://formy-project.herokuapp.com/autocomplete");

WebElement autocomplete = driver.findElement(By.id("autocomplete"));
autocomplete.sendKeys("110 university Ave w, Waterloo, ON");
Thread.sleep(1000); // attention here

WebElement autoResult = driver.findElement(By.className("pac-item"));
autoResult.click();


driver.quit();
}
}

如果报错,就是脚本跑太快了,没有预留足够的时间让网页弹出选项。

  1. Scroll to the element

    对于测试来说,如果要测试一个没有显示出来(需要滚动屏幕)的元素,滚动屏幕这个动作并不是必须的。因为对于网页来说,无论在不在屏幕范围内,html里都是有这个元素的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Scroll {
public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "/Users/shuolintian/Downloads/chromedriver");

WebDriver driver = new ChromeDriver();

driver.get("https://formy-project.herokuapp.com/scroll");

WebElement name = driver.findElement(By.id("name"));
Actions action = new Actions(driver);
action.moveToElement(name);
Thread.sleep(1000);
name.sendKeys("Shuolin");

WebElement date = driver.findElement(By.id("date"));
date.sendKeys("11-22-2020");


driver.quit();
}
}
  1. Switch to active window
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class SwitchToActiveWindow {
public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "/Users/shuolintian/Downloads/chromedriver");

WebDriver driver = new ChromeDriver();

driver.get("https://formy-project.herokuapp.com/switch-window");

WebElement newTabButton = driver.findElement(By.id("new-tab-button"));
newTabButton.click();

Set<String> originalHandle = driver.getWindowHandles();

for(String handle1: driver.getWindowHandles()) {
driver.switchTo().window(handle1);
}

driver.switchTo().window(String.valueOf(originalHandle));

driver.quit();
}
}
  1. switch to alert
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class SwitchToAlert {
public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "/Users/shuolintian/Downloads/chromedriver");

WebDriver driver = new ChromeDriver();

driver.get("https://formy-project.herokuapp.com/switch-window");

WebElement alertButton = driver.findElement(By.id("alert-button"));
alertButton.click();

Alert alert = driver.switchTo().alert();
alert.accept(); // accept is a method to close the alert

driver.quit();
}
}
  1. JavaScript Excecuter

    When to use:

    1. When WebDriver fails to click on any element
    2. To trigger actions on a page
    3. To perform movement on a page, like scroll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class ExecuteJavascript {
public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "/Users/shuolintian/Downloads/chromedriver");

WebDriver driver = new ChromeDriver();

driver.get("https://formy-project.herokuapp.com/modal");

WebElement modalButton = driver.findElement(By.id("modal-button"));
modalButton.click();

WebElement closeButton = driver.findElement(By.id("close-button"));

// the following two lines equal to closeButton.click();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", closeButton);

driver.quit();
}
}

Using CSS selector to find elements

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 1. by Id
driver.findElement(By.cssSelector("#idName"));
// 2. by class
driver.findElement(By.cssSelector(".className"));
// 3. by tag
driver.findElement(By.cssSelector("#tagName"));
// 4. by attribute
driver.findElement(By.cssSelector("tagName[attribute=value]"));
// 5. tag+class
driver.findElement(By.cssSelector("tagName.className"));
// 6. tag+id #:hash/number sign
driver.findElement(By.cssSelector("tagName#idName"));
// 7. multiple class
driver.findElement(By.cssSelector(".className1.className2.className3"));

Match Text

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 1. by exact Match
<div id="textField">
driver.findElement(By.cssSelector("div[id='textField']"));

// 2. by Prefix
<div id="textField_randomId">
driver.findElement(By.cssSelector("div[id^='textField']"));

// 3. by suffix
<div id="randomId_textField">
driver.findElement(By.cssSelector("div[id$='textField']"));

// 4. by substring
<div id="123_textField_randomId">
driver.findElement(By.cssSelector("div[id*='textField']"));

Child Node

1
2
3
4
5
6
7
<ul id="list">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>

driver.findElement(By.cssSelector("#list li:nth-child(n)"))
Shuolin Tian wechat
欢迎加我微信交流