Selenium
代码
test_by_selenium.py
# coding=utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from util import drivers
import pytest
from selenium.webdriver.common.by import By
class TestSelenium():
def setup(self):
d = drivers()
self.driver=d.driver
self.driver.get("http://www.baidu.com")
def test_baidu(self):
inputElement = self.driver.find_element(By.NAME, "wd")
inputElement.send_keys("软件测试")
inputElement.submit()
WebDriverWait(self.driver, 20).until(lambda driver: driver.title.startswith("软件测试"))
assert self.driver.title.startswith("软件测试")
def teardown(self):
self.driver.quit()
if __name__ == '__main__':
pytest.main(["-v", "TestSelenium.py"])
util.py
#!/usr/bin/env python
#coding:utf-8
from selenium import webdriver
import time
from selenium.webdriver.edge.service import Service
class drivers:
def __init__(self):
f = open("info.txt", "r")
driverPach = "C:\\Lib\\";
brower = f.readline()
f.close()
if brower.lower() == "ie":
service = Service(executable_path=driverPach+"IEDriverServer.exe")
options = webdriver.IEOptions()
elif brower.lower() == "firefox":
service = Service(executable_path=driverPach+"geckodriver.exe")
options = webdriver.FirefoxOptions()
elif brower.lower() == "chrome":
service = Service(executable_path=driverPach+"chromedriver.exe")
options = webdriver.ChromeOptions()
elif brower.lower() == "edge":
service = Service(executable_path=driverPach+"msedgedriver.exe")
options = webdriver.EdgeOptions()
else:
print("info.txt中的信息有误")
options.add_argument('--start-maximized')
options.service = service
if brower.lower() == "ie":
self.driver = webdriver.IE(options=options)
elif brower.lower() == "firefox":
self.driver = webdriver.Firefox(options=options)
elif brower.lower() == "chrome":
self.driver = webdriver.Chrome(options=options)
elif brower.lower() == "edge":
self.driver = webdriver.Edge(options=options)
else:
print("info.txt中的信息有误")
self.driver.maximize_window()
Playwright
代码
import pytest
from playwright.sync_api import Page
def test_baidu(page: Page):
inputStr="软件测试"
page.goto("https://www.baidu.com/")
page.locator("#kw").click()
page.locator("#kw").fill(inputStr)
page.get_by_role("button", name="百度一下").click()
page.wait_for_selector('span[class="hint_PIwZX c_font_2AD7M"]',state='attached')
assert page.title() == inputStr+ '_百度搜索'
assert page.text_content('#s_tab>div>a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic') == '图片'
page.close()
if __name__ == '__main__':
pytest.main(["-v", "TestPlaywright.py"])
cypress
describe('百度',function(){
context('百度测试',function(){
it('进入百度成功,输入软件测试',function (){
cy.visit('https://www.baidu.com/')
cy.get('#kw').type('软件测试')
cy.get('#su').click()
cy.get('a[name="tj_settingicon"]').should('contain', '设置')
})
})
})
结果
| 第1次 | 第2次 | 第3次 | 第4次 | 第5次 | 平均 |
selenium | 6.99s | 6.90s | 6.93s | 6.73s | 6.90s | 6.87s |
Playwright | 4.23s | 4.23s | 4.42s | 4.24s | 4.35s | 4.29s |
cypress | 5s | 4s | 5s | 5s | 4s | 4.6s |
由此可见,selenium运行速度最慢,Playwright与cypress速度相当
最后:下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】
软件测试面试文档
我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。