Selenium高手必备:基于WebDriver的Web UI自动化
Posted 软件测试呀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Selenium高手必备:基于WebDriver的Web UI自动化相关的知识,希望对你有一定的参考价值。
Selenium是目前最流行的Web UI自动化测试框架。熟悉Selenium的人也知道Selenium是基于WebDriver的。
那么在没有Selenium的情况下,我们能直接调用WebDriver实现Web UI的自动化吗?答案当然是肯定的。本文将带您实现基于网络驱动的网络用户界面自动化。
本文通过直接调用Selenium、Curl命令和ChromeDriver来实现同样的功能。
编程语言为C#,已经通过了Visual Studio 2019中的测试,其他主流编程语言也可以完成同样的功能。
对比三种实现方法,我们很容易理解如何直接调用WebDriver来完成Web UI的自动化,而不需要Selenium。
在阅读以下内容之前,您需要具备Selenium和WebDriver的基本知识。
手动步骤
1.打开Chrome浏览器
2.进入
https://www.baidu.com
主页
3.搜索框输入“Selenium”
4.点击“百度一下”
5.关闭Chrome浏览器
调用Selenium的C#代码
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace HelloSelenium
class Program
static void Main(string[] args)
IWebDriver driver = null;
try
//1. 打开Chrome浏览器
driver = new ChromeDriver();
//2. 进入https://www.baidu.com/主页
driver.Navigate().GoToUrl("https://www.baidu.com/");
//3. 搜索框输入“Selenium”
driver.FindElement(By.Id("kw")).SendKeys("Selenium");
//4. 点击“百度一下”
driver.FindElement(By.Id("su")).Click();
finally
//5. 关闭Chrome浏览器
if (driver != null)
driver.Dispose();
Curl命令
打开Chrome浏览器
启动命令提示符,执行chromedriver.exe --port=9515 (注意选用与Chrome版本对应的chromedriver版本,端口只要未被占用即可)。
另起一个命令提示符,执行curl命令(注意端口号),记住返回的sessionId。
curl -d @JsonFile1.json http://localhost:9515/session
JsonFile1.json内容:
"desiredCapabilities":
"caps":
"nativeEvents": false,
"browserName": "chrome",
"version": "",
"platform": "ANY"
进入
https://www.baidu.com
主页
curl -d @JsonFile2.json http://localhost:9515/session/36d903cbd2177c278b5d39bbe74a3318/url
JsonFile2.json内容:
“url”:“https://www. baidu. com/”
搜索框输入“Selenium”
获取elementId:
curl -d @JsonFile3.json http://localhost:9515/session/36d903cbd2177c278b5d39bbe74a3318/element
JsonFile3.json内容:
“using”:“css selector”,“value”:“#kw”
输入“Selenium”:
curl -d @JsonFile4.json http://localhost:9515/session/36d903cbd2177c278b5d39bbe74a3318/element/0.7861531328870939-1/value
JsonFile4.json内容:
“value”:[“Selenium”]
点击“百度一下”
获取elementId:
curl -d @JsonFile5.json http://localhost:9515/session/36d903cbd2177c278b5d39bbe74a3318/ele
JsonFile5.json内容:
“using”:“css selector”,“value”:“#su”
点击:
curl -d @JsonFile4.json curl -d @JsonFile6.json http://localhost:9515/session/36d903cbd2177c278b5d39bbe74a3318/element/0.7861531328870939-2/click
JsonFile6.json内容:
关闭Chrome浏览器
关闭Chrome:
关闭Chrome
curl -X DELETE http://localhost:9515/session/36d903cbd2177c278b5d39bbe74a3318
关闭chromedriver.exe:
curl http://localhost:9515/shutdown
调用ChromeDriver的C#代码
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace HelloSelenium
class Program
static IList<HttpCmd> cmdArr;
static readonly string baseUrl = "http://localhost:9515/";
static readonly string extendUrlFormat1 = "session";
static readonly string extendUrlFormat2 = "session/sessionId/url";
static readonly string extendUrlFormat3 = "session/sessionId/element";
static readonly string extendUrlFormat4 = "session/sessionId/element/elementId/value";
static readonly string extendUrlFormat5 = "session/sessionId/element";
static readonly string extendUrlFormat6 = "session/sessionId/element/elementId/click";
static readonly string extendUrlFormat7 = "session/sessionId";
static readonly string extendUrlFormat8 = "shutdown";
static readonly string jsonData1 = @"""desiredCapabilities"": ""caps"": ""nativeEvents"": false, ""browserName"": ""chrome"", ""version"": """",""platform"": ""ANY""";
static readonly string jsonData2 = @"""url"":""https://www.baidu.com/""";
static readonly string jsonData3 = @"""using"":""css selector"",""value"":""#kw""";
static readonly string jsonData4 = @"""value"":[""Selenium""]";
static readonly string jsonData5 = @"""using"":""css selector"",""value"":""#su""";
static readonly string jsonData6 = @"";
static Dictionary<string, string> dicSe = new Dictionary<string, string>()
"sessionId", null;
static Dictionary<string, string> dicSeEl = new Dictionary<string, string>()
"sessionId", null,"elementId", null;
static Program()
cmdArr = new List<HttpCmd>()
new HttpCmd(null, null, null, null),
new HttpCmd("POST", extendUrlFormat1, jsonData1, null),
new HttpCmd("POST", extendUrlFormat2, jsonData2, dicSe),
new HttpCmd("POST", extendUrlFormat3, jsonData3, dicSe),
new HttpCmd("POST", extendUrlFormat4, jsonData4, dicSeEl),
new HttpCmd("POST", extendUrlFormat5, jsonData5, dicSe),
new HttpCmd("POST", extendUrlFormat6, jsonData6, dicSeEl),
new HttpCmd("DELETE", extendUrlFormat7, null, dicSe),
new HttpCmd("GET", extendUrlFormat8, null, null),
;
static void Main(string[] args)
Process p = null;
try
string response = null, sessionId = null, elementId = null, extendUrl;
JObject jObj = null;
//1. 打开Chrome浏览器
//启动chromedriver.exe
p = Process.Start(@"D:\\Software\\chromedriver.exe", "--port=9515");
Thread.Sleep(1000);
//启动chrome
response = HttpOp(cmdArr[1]);
jObj = JsonConvert.DeserializeObject(response) as JObject;
sessionId = jObj["sessionId"].Value<string>();
dicSe["sessionId"] = sessionId;
dicSeEl["sessionId"] = sessionId;
Thread.Sleep(1000);
//2. 进入https://www.baidu.com/主页
HttpOp(cmdArr[2]);
Thread.Sleep(1000);
//3. 搜索框输入“Selenium”
//获取elementId
response = HttpOp(cmdArr[3]);
jObj = JsonConvert.DeserializeObject(response) as JObject;
elementId = jObj["value"]["ELEMENT"].Value<string>();
dicSeEl["elementId"] = elementId;
//输入“Selenium”
HttpOp(cmdArr[4]);
Thread.Sleep(1000);
//4. 点击“百度一下”
//获取elementId
response = HttpOp(cmdArr[5]);
jObj = JsonConvert.DeserializeObject(response) as JObject;
elementId = jObj["value"]["ELEMENT"].Value<string>();
dicSeEl["elementId"] = elementId;
//点击
HttpOp(cmdArr[6]);
Thread.Sleep(1000);
//5. 关闭Chrome浏览器
//关闭Chrome
HttpOp(cmdArr[7]);
//关闭chromedriver
HttpOp(cmdArr[8]);
finally
if (p != null)
p.WaitForExit(3000);
p.Dispose();
private static string HttpOp(HttpCmd cmd)
var fullUrl = baseUrl + cmd.ExtendUrl;
HttpClient client = new HttpClient();
Task<HttpResponseMessage> response = null;
switch (cmd.Method)
case "GET":
response = client.GetAsync(fullUrl);
break;
case "DELETE":
response = client.DeleteAsync(fullUrl);
break;
case "POST":
HttpContent content = new StringContent(cmd.JsonData, Encoding.UTF8, "application/json");
response = client.PostAsync(fullUrl, content);
break;
return response.Result.Content.ReadAsStringAsync().Result;
internal class HttpCmd
public string Method get; set;
public string ExtendUrlFormat get; set;
public string JsonData get; set;
public Dictionary<string, string> ParaDictionary get; set;
public string ExtendUrl => BuildExtendUrl();
public HttpCmd(string method, string extendUrlFormat, string jsonData, Dictionary<string, string> paraDictionary)
this.Method = method;
this.ExtendUrlFormat = extendUrlFormat;
this.JsonData = jsonData;
this.ParaDictionary = paraDictionary;
private string BuildExtendUrl()
var extendUrl = ExtendUrlFormat;
if (ParaDictionary != null && ParaDictionary.Count > 0)
foreach (var pair in ParaDictionary)
extendUrl = extendUrl.Replace(pair.Key, pair.Value);
return extendUrl;
总结
Curl命令就是Http调用WebDriver命令。
调用ChromeDriver的代码也是Http调用WebDriver命令,调用Selenium的代码实际上还是Http调用WebDriver命令。只不过不是直接调用,而是通过Selenium去执行WebDriver命令。Selenium封装了WebDriver。
至于Selenium具体是怎么封装WebDriver的,内容较多,本文不做这部分的分析。
使用直接Http调用WebDriver命令的方式来做Web UI自动化,写代码很麻烦,实用价值很低。本文并不鼓励大家在实际工作中使用这样的方式来做自动化。
但是,如果你在工作中经常用到Selenium,想具体了解Selenium的原理,成为高手,学习这种方式是必要的。
这种方式非常有助于深入学习和理解Selenium,是Selenium高手所必备的知识和技能。
房子要一层一层盖,知识要一点一点学。大家在学习过程中要好基础,多上手实操,话不多说,这里狠狠上一次干货!我熬夜整理好的各阶段(功能、接口、自动化、性能、测开)技能学习资料+实操讲解,非常适合私下里学习,比找资料自学高效多了,分享给你们。
领取关 w/x/g/z/h:软件测试小dao
敲字不易,如果此文章对你有帮助的话,点个赞收个藏来个关注,给作者一个鼓励。也方便你下次能够快速查找。
以上是关于Selenium高手必备:基于WebDriver的Web UI自动化的主要内容,如果未能解决你的问题,请参考以下文章
基于python实现UI自动化3.0 selenium - webdriver常见8大元素定位
在Jmeter中使用Selenium WebDriver完成测试
基于webdriver的jmeter性能测试-Selenium IDE