selenium自动化使用技巧

作者: 西门叔媛, 发布: 2024-09-20 04:11:23

手把手教会你selenium无代码自动化测试工具SeleniumIDE最新更新的亮点是其自动化无代码测试工具——Selenium命令行执行器,它简化了测试执行过程。 无需写干净的代码,所有IDE测试用例都可以通过命令行作在浏览器中运行,并且支并发执行。
首先确保你的系统已经安装了Node.js环境(具体安装步骤在前面的教程中有详细介绍)。 使用淘宝镜像cnpm安装:运行命令npminstall-gcnpm--registry=registry.npm.taobao.org。 接下来,安装Firefox和Chrome浏览器驱动程序,例如cnpminstall-ggeckodriver和cnpminstall-gchromedriver。
在Firefox浏览器中开SeleniumIDE扩展,录测试脚本并保存为168test.side文件。 进行无代码测试的步骤是输入命令行:selenium-side-runner-c"browserName=specifydriverbrowser"'scriptpath'。 例如,运行Firefox脚本的命令为:selenium-side-runner-c"browserName=firefox"C:\\Users\\kk\\Desktop\\168test.side。
如果需要批量运行多个测试用例,只需更改包含测试用例的文件夹的路径即可,例如C:\\Users\\kk\\Desktop\\testcase\\*.side,该文件夹下的所有side文件可以同时运行。

自动化selenium基本作方法总结#定位UI元素(WebElements)
find_element_by_id
find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector
#获取元素数据
ele.get_attribute('href')
ele.text
ele.inner
ele.get_attribute('outerHTML')
ele.get_attribute('innerHTML')
#导入ActionChains类
fromselenium.webdriverimportActionChains
#鼠标移动到ac位置
ac=driver.find_element_by_xpath('element')
ActionChains(driver).move_to_element(ac).perform()
#在ac位置点击
ac=driver.find_element_by_xpath("elementA")
ActionChains(driver).move_to_element(ac).click(ac).perform()
#双击ac位置
ac=driver.find_element_by_xpath("elementB")
ActionChains(driver).move_to_element(ac).double_click(ac).perform()
#在ac位置右键单击
ac=driver.find_element_by_xpath("elementC")
ActionChains(司机)。 move_to_element(ac).context_click(ac).perform()
#在ac位置左键单击按住
ac=driver.find_element_by_xpath('elementF')
ActionChains(driver).move_to_element(ac).click_and_hold(ac).perform()
#将ac1到ac2位置
ac1=driver.find_element_by_xpath('elementD')
ac2=driver.find_element_by_xpath('elementE')
ActionChains(driver).drag_and_drop(ac1,ac2).perform()
#导入Select类
fromselenium.webdriver.support.uiimportSelect
select=Select(driver.find_element_by_name('status')
select.select_by_visible_text("审核失败")
#页面切换
driver.switch_to.window("窗口名称")
#作页面前进和后退
driver.forward()
driver.back()
#页面等待
##隐式等待
driver.implicitly_wait(10)
##显示等待
try:#页面不断循环,直到id="myElement"出现
element=WebDriverWait(driver,10).until(EC.pre sence_of_element_located((By.ID,"myElement")))
最后:
driver.quit()

相关文章