python + Poium library operations

1, installation support pip

pip install poium

2, Basic Usage

from poium import PageElement,Page,PageElements

# 1.poium支持的8种定位方法

class SomePage(Page):
elem_id = PageElement(id_='id')
elem_name = PageElement(name_='name')
elem_class = PageElement(class_name='class')
elem_tag = PageElement(tag='input')
elem_link_text = PageElement(link_text='this is link')
elem_partial_link_text = PageElement(partial_link_text='is link')
elem_xpath = PageElement(xpath='//*[@id="kw"]')
elem_css = PageElement(css='#id')

# 2.设置元素超时时间 默认为10s

class BaiduPage(Page):
search_input = PageElement(id_='kw',timeout=5)
search_button = PageElement(id_='su',30 = timeout)

# 3. Set element describes
# When the elements of a Page class defined in a very long time, it is necessary to increase the readability by the comment, then you can use the describe parameter
# stressed: describe moot argument, but adds the element positioning readability

class LoginPage ( Page):
'' 'Login Page class' ''
username = pageelement (CSS = '# loginAccount', DESCRIBE = 'username')
password = pageelement (CSS = '# loginpwd', DESCRIBE = 'password')
login_button = pageelement (css = '# login_btn', describe = ' Login button')
USER_INFO = pageelement (CSS = 'a.nav_user_name> span', DESCRIBE = 'user information')


# 4 targeting a set of elements

class ResultPage (Page):
# positioning a set of elements
search_input = PageElements (xpath = '// div / h3 / a')

3, the basic use

Use poium rewrite baidu_page.py

Page poium Import from, PageElement 

'' '
create BaiduPage class, it inherits poium library Page class, call the class definition PageElement element positioning,
and assigned to the variable search_input and search_button.
'' '

Class BaiduPage (Page):
' '' Page Baidu Baidu layer packaging operations to page elements' ''
SEARCH_INPUT = pageelement (ID_ = 'kW')
search_button = pageelement (ID_ = 'SU')

test cases using the following :
from TestCase.Poium.baidu_page import BaiduPage
from selenium import webdriver
from time import sleep
import unittest


class TestBaidu(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.driver = webdriver.Chrome()

@classmethod
def tearDownClass(cls) -> None:
cls.driver.close()

def test_baidu_search_casel(self):
'''测试百度搜索'''
page = BaiduPage(self.driver)
page.get('http://www.baidu.com')
page.search_input = 'selenium'
page.search_button.click()
sleep(2)
    #断言
self.assertEqual(page.get_title,'selenium_ Baidu search ')

if __name__ == '__main__':
unittest.main(verbosity=2)


Guess you like

Origin www.cnblogs.com/Teachertao/p/11900682.html