Fork me on GitHub
Shuolin's Blog

动手又动脑,才能有创造


  • Home

  • About

  • Tags

  • Categories

  • Archives

  • Sitemap

  • Like

  • Search

Selenium 学习笔记

Posted on 2020-01-10 |

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)"))

如何上传博客源代码到Github分支

Posted on 2019-12-05 |
网上有很多相互抄来抄去的文章,错误一大堆,无法复现,找了很久,终于发现一个正确的方法,故自己总结再分享之。
Read more »

二叉树的深度遍历(前序,中序,后序)和广度遍历(层次遍历)以及 Morris Traversal

Posted on 2019-12-03 |
二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的。对于二叉树,有深度遍历(DFS)和广度遍历(BFS),深度遍历有前序(preorder)、中序(inorder)以及后序(postorder)三种遍历方法,广度遍历即我们平常所说的层次遍历。因为树的定义本身就是递归定义,因此采用递归的方法去实现树的三种遍历不仅容易理解而且代码很简洁,而对于广度遍历来说,需要其他数据结构的支撑,比如堆或者队列。
Read more »

实现完全二叉搜索树

Posted on 2019-12-03 |
我们先考虑如何转换成一个BST,而不是complete BST。我们知道一个二叉搜索树在inorder的顺序下是一个递增序列,所以突破口就在于可以先将数列排序好,就相当于我们知道了树的inorder顺序,然后我们将其转化为level-order的顺序输出就可以了。那么如何转换呢?
Read more »

191. Number of 1 Bits

Posted on 2019-12-02 |

Easy

Write a function that takes an unsigned integer and return the number of ‘1’ bits it has (also known as the Hamming weight).

Read more »

128. Longest Consecutive Sequence

Posted on 2019-12-02 |

Hard

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

Your algorithm should run in O(n) complexity.

Example:

1
2
3
Input: [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
Read more »

2.Add Two Numbers

Posted on 2019-12-02 |

Medium

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

1
2
3
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
Read more »

1. Two Sum

Posted on 2019-12-02 |

Easy

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

1
2
3
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
Read more »

3. Longest Substring Without Repeating Characters

Posted on 2019-12-02 |

JAVA刷题ing

Medium

Given a string, find the length of the longest substring without repeating characters.

Example 1:

1
2
3
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

1
2
3
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

1
2
3
4
Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
Read more »

130. Surrounded Regions

Posted on 2019-12-02 |

Medium

Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.

A region is captured by flipping all 'O's into 'X's in that surrounded region.

Example:

1
2
3
4
X X X X
X O O X
X X O X
X O X X

After running your function, the board should be:

1
2
3
4
X X X X
X X X X
X X X X
X O X X

Explanation:

Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically

Read more »
12…6
Shuolin Tian

Shuolin Tian

人有两个宝,双手和大脑。

52 posts
14 tags
GitHub E-Mail FB Page Instagram
© 2019 — 2020 Shuolin Tian
Powered by Hexo
|
Theme — NexT.Pisces v5.1.4
人次 次