第九期_Web 自动化测试 Selenium 框架_20190612

有问题可随时回帖

Selenium

Selenium 3.0.0.b1
* Remove Selenium RC support

Selenium架构

  • Appium == Selenium Server
  • ChromeDriver == ChromeDriver
  • Appium Client <= Selenium Client
  • Appium Desktop == Selenium IDE
  • Selenium Grid 支持所有的webdriver的框架 appium selenium

Selenium IDE

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Mtsc2019(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.katalon.com/"
        self.verificationErrors = []
        self.accept_next_alert = True
    
    def test_mtsc2019(self):
        driver = self.driver
        driver.get("https://testerhome.com/topics/19462")
        driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Toggle'])[1]/following::b[1]").click()
        driver.find_element_by_link_text(u"MTSC2019 大会日程 V1.0 版公布,投票你最关注的议题抽奖大会门票!").click()
        driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='simple'])[1]/following::button[1]").click()
        driver.find_element_by_link_text(u"金数据投票表单:").click()
        # ERROR: Caught exception [ERROR: Unsupported command [selectFrame | index=0 | ]]
        driver.find_element_by_link_text("http://2019.test-china.org/").click()
        # ERROR: Caught exception [ERROR: Unsupported command [selectWindow | win_ser_1 | ]]
    
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e: return False
        return True
    
    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True
    
    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True
    
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

Selenium WebDriver

下载每个浏览器对应的驱动,并放到PATH变量下,或者通过修改executable_path参数来解决

Firefox驱动

geckodriver 0.24.0 ( 2019-01-28)

The source code of this program is available from
testing/geckodriver in https://hg.mozilla.org/mozilla-central.

This program is subject to the terms of the Mozilla Public License 2.0.
You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.

Chrome 驱动

下载跟你的浏览器对应的版本

ChromeDriver 74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29})

元素定位

chrome的定位技巧,在console下可以执行两个特别的函数

$x('xpath表达式')
$x('//*[@data-toggle="dropdown" and @class="btn btn-default"]')
$x('//*[text()[contains(., "目录")]]')

$('css表达式')
$('.toc-container .btn.btn-default')
$('.btn.btn-default[data-toggle=dropdown]')

js定位
document.getElementById
document.getElementsByXXX

css定位表达式学习:https://www.w3schools.com/cssref/css_selectors.asp
XPath定位: XPath Syntax

作业

  • 完成雪球的搜索,搜索阿里巴巴,选择1688,并添加添加。输入错误的用户名和密码就可以。把代码贴到回复里