Automation of the second package web python3

Did web automation interface to automate or students know, the only difficulty is that the elements to locate, acquire text, as well as pop-up messages, authentication code, toast, etc., and there are eight elements positioning method

We can conduct a classification of these eight methods. Form their own function.

 

To the web, for example:

Selenium the webdriver Import from 
Import the unittest


class the MyTest (of unittest.TestCase):
DEF the setUp (Self):
self.driver = webdriver.Chrome ()

DEF the tearDown (Self):
self.driver.quit ()

DEF my_open (Self, URL):
self.driver.get (URL)

DEF the Click (Self, style, text):
self.driver.find_element (style, text) .click ()

DEF send_keys (Self, style, text, Key):
self.driver.find_element ( style, text) .send_keys (key)

in front of the browser is open, other operations, the use case when too much time, and only when the link add decorator, do not always open a browser the following way:

class Test(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.url = "https://mail.qq.com/"

@classmethod
def tearDownClass(cls):
cls.driver = webdriver.Chrome()
cls.driver.quit()

def login(self, username, password):
self.driver.get(self.url)
sleep(2)
self.driver.maximize_window() # 将浏览器最大化显示
sleep(1)
self.driver.find_element_by_id("switcher_plogin").click()
self.driver.find_element_by_css_selector("input[name='u']").send_keys(username)
self.driver.find_element_by_css_selector("input[name='p']").send_keys(password)
self.driver.find_element_by_id("login_button").click()
sleep(2)

def test_username_null(self):
"""
测试用户名为空
:return:
"""
self.driver.get(self.url)
self.login("","123")
sleep(2)
result=self.driver.find_element_by_id("err_m").text
self.assertEqual(result , "你还没有输入帐号!")


def test_password_null(self):
"""
测试密码为空
:return:
"""
self.driver.get(self.url)
self.login("error","")
= self.driver.find_element_by_id the Result ( "err_m"). text
Print (the Result)

written when the test is performed, it will save a lot of time.


When specific calls:

first of all necessary to import class came Mytest

# - * - Coding: UTF-8 - * - 
from test_dir.e * Import
from SLEEP Time Import
Import unittest


class the Test (MyTest):

DEF test_case1 (Self):
self.my_open ( "http://www.baidu.com ")
self.send_keys (style =" ID ", text =" kW ", Key =" Selenium ")
SLEEP (2)
self.click (style = 'XPath', text = '// * [@ ID =" SU "] ')
SLEEP (. 3)
Result = self.driver.find_element_by_xpath (' // * [@ ID =". 1 "] / H3 / A '). text
self.assertEqual (Result," automates the Selenium browsers. That apos IT! ")
directly from self to call methods in the parent class, what send_keys ah there clicl and so

we have a good opinion welcomed the discussion! ! !











Guess you like

Origin www.cnblogs.com/testling/p/12022185.html