赵走x博客
网站访问量:151569
首页
书籍
软件
工具
古诗词
搜索
登录
5、TestCase类
4、unittest单元测试框架
3、支持跨浏览器
2、 第一个Selenium Python脚本
1、基于Python的Selenium WebDriver入门
3、支持跨浏览器
资源编号:75970
Selenium自动化测试:基于Python语言
书籍
热度:107
目前我们已经在Firefox浏览器构建并运行了脚本。Selenium支持各种浏览器,读者可以在不同的浏览器中进行自动化测试。
目前我们已经在Firefox浏览器构建并运行了脚本。Selenium支持各种浏览器,读者可以在不同的浏览器中进行自动化测试。 它支持的浏览器包括IE浏览器、Google Chrome浏览器、Safari浏览器、Opera浏览器,甚至是像PhantomJS这样的无UI界面的浏览器。接下来的部分,我们会修改刚才创建的脚本,以便在IE浏览器和Google Chrome浏览器中运行脚本,以此来验证Selenium WebDriver跨浏览器的兼容性。 # 1、设置IE浏览器 在IE浏览器中运行脚本步骤会多一些。我们需要下载并安装InternetExplorerDriver。 InternetExplorerDriver是一个独立的可执行的服务,它实现WebDriver的协议,使得WebDriver可以与测试脚本和IE浏览器交互。InternetExplorerDriver支持Windows XP、Vista、Windows 7和Windows 8操作系统下的主要IE版本。通过以下步骤安装InternetExplorerDriver。 * 1)在http://www.seleniumhq.org/download/ 中下载InternetExplorerDriver服务。你可以根据自己的 操作系统来选择下载32位或64位版本。 * 2)在下载完成后,解压文件,并把文件复 制到存储脚本的目录中。 * 3)在IE 7及其以上版本,每个区域的保护模式设置一定要有相同的值。在每个区域中保护模式要么启用,要么关闭。设置保护模式的步骤如下。 ① 在工具菜单下选择Internet选项。 ② 在Internet选项对话框中,单击安全标签页。 ③ 在“选择区域以查看或更改安全设置”中选择每一个区域,确定每个区域的保护模式的值保持一致(要么选中,要么不选中)。所有区域用相同的设置,如下图所示。  在使用InternetExplorerDriver时,注意保持浏览器缩放等级设置成100%,以此来保证鼠标的单击事件能点到正确的坐标。 * 4)修改脚本使其支持IE浏览器。我们通过以下方式来使用IE替代Firefox实例。 ``` from selenium import webdriver import os dir=os.path.dir(__file__) ie_driver_path=dir+'/IEDriverServer.exe' # create a new Firefox session driver = webdriver.Ie(ie_driver_path) driver.implicitly_wait(30) driver.maximize_window() # navigate to the application home page driver.get("http://demo.magentocommerce.com") # get the seach textbox search_field = driver.find_element_by_name("keys") search_field.clear() # enter search keyword and submit search_field.send_keys("phones") search_field.submit() # get all the anchor elements which have product names displayed # currently on result page using find_elements_by_xpath method products = driver.find_element_by_xpath("//h2[@class='product-name']/a") # get the number of anchor elements found print("Found {} products:".format(len(products))) # iterate through each anchor element and print the text # that is name of the produt for product in products: print(print.text) # close the browser window driver.quit() ``` 在这个脚本中,在创建IE浏览器实例时,我们传递了InternetExplorerDriver的路径。 * 5)运行脚本后,Selenium会加载InternetExplorerDriver服务,用它来启动浏览器和执行脚本。 InternetExplorerDriver服务在Selenium脚本和浏览器之间扮演类似中介角色。实际执行的步骤与我们在Firefox浏览器观察的类似。 >在https://code.google.com/p/selenium/wiki/InternetExplorerDriver 中可以获取更多关于IE的重要设置。 在https://code.google.com/p/selenium/wiki/DesiredCapabilities 查阅DesiredCapabilities的文章。 # 2、设置Google Chrome浏览器 在Google Chrome浏览器中设置和运行脚本的步骤与IE浏览器的相似。我们需要下载ChromeDriver服务。ChromeDriver服务是一个由Chromium team开发维护的独立的服务,它支持Windows操作系统、Linux操作系统和Mac操作系统。使用以下步骤来设置ChromeDriver服务。 * 1)在http://chromedriver.storage.googleapis.com/index.html 下载ChromeDriver服务。 * 2)下载完ChromeDriver服务后,解压文件,并把文件复制到存储脚本的目录中。 * 3)修改脚本使其支持Chrome浏览器。我们通过以下方式创建Chrome实例,用此来替换Firefox浏览器实例。 ``` from selenium import webdriver import os dir=os.path.dir(__file__) chrom_driver_path=dir+'/chromedriver.exe' # create a new Firefox session driver = webdriver.Chrome(chrom_driver_path) driver.implicitly_wait(30) driver.maximize_window() # navigate to the application home page driver.get("http://demo.magentocommerce.com") # get the seach textbox search_field = driver.find_element_by_name("keys") search_field.clear() # enter search keyword and submit search_field.send_keys("phones") search_field.submit() # get all the anchor elements which have product names displayed # currently on result page using find_elements_by_xpath method products = driver.find_element_by_xpath("//h2[@class='product-name']/a") # get the number of anchor elements found print("Found {} products:".format(len(products))) # iterate through each anchor element and print the text # that is name of the produt for product in products: print(print.text) # close the browser window driver.quit() ``` 在这个脚本中,在创建Chrome浏览器实例时,我们传递了ChromeDriver的路径。 * 4)运行脚本后,Selenium会加载ChromeDriver服务,用它来启动浏览器和执行脚本。 实际执行的步骤与我们在Firefox浏览器观察的类似。 >想了解更多关于ChromeDriver,请访问https://code.google.com/p/selenium/wiki/ChromeDriver 和 https://sites.google.com/a/chromium.org/chromedriver/home 。 # 4、章节回顾 在本章中,我们介绍了Selenium和它的组件。通过pip工具安装了Selenium包。然后我们通过创建基于示例 程序的测试脚本,成功运行在Firefox浏览器,并分析了整个过程。最后,我们举一反三(分别在IE浏览器和Chrome浏览器中配置和运行脚本)来验证Selenium WebDriver的跨浏览器的特性。 在下一章,我们将学习如何通过Selenium WebDriver使用unittest库来创建自动化单元测试。我们也将学习如何创建并运行一组测试脚本。