【30K上岸京东测试岗】全靠了这套python自动化测试全栈测试开发技术入门到精通教程。
1、环境安装
nodejs安装
webdriver安装,安装各浏览器的驱动,通过各浏览器的驱动程序,操作浏览器。
chrome 驱动安装
每个版本的浏览器用到的webdriver不同。可以通过chrome右上角-> 帮助 -> 关于 Chrome 查看当前版本。
chromedriver下载地址和chromedriver与chrome之间的对应关系。跳转门
其他浏览器驱动可以见下面列表:
- firefox(火狐浏览器):github.com/mozilla/gec…
- Edge:developer.microsoft.com/en-us/micrs…
- Safari:webkit.org/blog/6900/w…
2、代码实现自动化测试
-
npm init
-
npm install selenium-webdriver
-
// index.js
-
const {Builder, By, Key, until} = require('selenium-webdriver');
-
const {Options} = require('selenium-webdriver/chrome');
-
var driver = new Builder()
-
.forBrowser('chrome')
-
.setChromeOptions(new Options().excludeSwitches(["ignore-certificate-errors", 'user-data-dir="C:\\Users\\qiang\\AppData\\Local\\Google\\Chrome\\User Data"']))
-
.build();
-
driver.get('https://image.baidu.com/')
-
.then(function () {
-
driver.findElement(By.id('search')).then(async (res)=>{
-
console.log('res',await res.getText())
-
})
-
}).catch(function (e) {
-
console.log(e);
-
});