Page Object Model (Selenium, Python)(一)
来源:互联网 发布:生产流程管理优化论文 编辑:程序博客网 时间:2024/05/16 09:52
Overview of Page Object Model
A page object represents an area in the web application user interface that your test is interacting with. Page objects reduces the amount of duplicated code and if the user interface changes, the fix needs changes in one place only.[1]
WHAT vs HOW
Usually the testers write the test cases describing ‘what’ is to be tested, this depends on the product functionality. But the implementation of this functionality by the developers keeps changing till the final code freeze is done, hence testers should know ‘how’ to implement the test cases so that the changes to the test scripts are minimal in case of code changes by the the developers. Page Objects encapsulates the finer details(locators and methods) of the pages from the test script and make the test script more readable and robust.
Sample Test Case – (WHAT)
We are going to explain about page objects with a very simple test case for Gmail.
-Goto http://gmail.com
-Enter the username, click Next
-Enter the password, click Sign in
-Perform search on the inbox ‘subject:POM’
-Click on the search result
-Click on inbox
A simple approach would be to write a test script with all the xpaths and the methods required for the execution of the above listed steps in one single file. The test would run fine and achieve the purpose but one major drawback is the test script is brittle. For any minor UI change on any page, the test script would have to be updated.To overcome this problem we use the page object pattern. As its name suggests,each page of the application to be tested is treated like an object which has the variables (xpaths) and methods (actions that can be performed on that particular page). This in turn makes the test script much cleaner.
Implementing the test case using POM templates (HOW)
Given below is the pictorial description of the various page objects used for the implementation of the test case.
Lets start with the main hero – Page.py
All page models can inherit from the Page class. This has useful wrappers for common Selenium operations
class Page(unittest.TestCase): "Page class that all page models can inherit from" def __init__(self,selenium_driver,base_url='https://mail.google.com/'): "Constructor" #We assume relative URLs start without a / in the beginning if base_url[-1] != '/': base_url += '/' self.base_url = base_url self.driver = selenium_driver #Visit and initialize xpaths for the appropriate page self.start() #Initialize the logger object self.log_obj = Base_Logging(level=logging.DEBUG) def open(self,url): "Visit the page base_url + url" url = self.base_url + url self.driver.get(url) def get_xpath(self,xpath): "Return the DOM element of the xpath OR the 'None' object if the element is not found" def click_element(self,xpath): "Click the button supplied" . . def write(self,msg,level='info'): self.log_obj.write(msg,level) def wait(self,wait_seconds=5): " Performs wait for time provided" time.sleep(wait_seconds)
Next is the Login_Page.py which handles the common functionality of user login. This will be the most re-used class.
Once we login, the main page is displayed which consists of the header (which contains the search box, user profile options),the navigation menu on the left side of the page and the main body. As the header and the navigation menu are common to all pages we created page objects for each of them. Here is a snippet of each of the classes.
Nav_Menu.py
Header_Section.py
Now, the Main_Page.py will contain the objects of the above two classes.
This completes the page objects needed for this particular test case.
**Please note – as an alternate way, we can also have a ‘Template_Page'(which inherits from the Page class and has the common objects) and have all pages(except Login page) derive from it.
In addition to these we have the following files
PageFactory.py
PageFactory uses the factory design pattern. get_page_object() returns the appropriate page object.
DriverFactory.py which returns the appropriate driver for firefox or chrome or IE browser.
login.credentials file contains the username , password required for authentication.
Finally , we have our test script which puts it all together and executes the test case.
Search_Inbox_Test.py
As you must have noticed the final test is very easy to read and need not be modified in case of any underlying changes to individual pages.
Running the test
Let us execute the test,
and here is the log file for the test run.
So now, we all agree that the page objects make it really easy for the tester to convert the documented test case to an automated test case. Not only that, maintaining these tests is also very easy.
原文:https://qxf2.com/blog/page-object-model-selenium-python/?utm_source=tuicool&utm_medium=referral
- Page Object Model (Selenium, Python)(一)
- Page Object Model (Selenium + Python)(二)
- Page Object Model (Selenium + Python)(三)
- Page Object Model (Selenium, Python)
- Selenium的PO模式(Page Object Model)[python版]
- Selenium的PO模式(Page Object Model)|(Selenium Webdriver For Python)
- Page Object Model (POM) & Page Factory in Selenium: Ultimate Guide
- Selenium:Page Object
- PO模式(Page Object Model)
- Python+Selenium使用Page Object实现页面自动化测试
- java-Selenium-Page Object设计模式思路
- python selenium学习(一)
- selenium+python 自动化(一)
- python+seleniu+page object使用
- Selenium-webdriver—使用Page Object设计模式
- Selenium使用Page Object实现页面自动测试
- Selenium:关于@FindBy在Page Object中的用法
- java-selenium自动化测试page object、pageFactory 设计模式
- Java位运算
- ubuntu16 环境配置
- chrome自动进入VM模式无法调试
- SSH全注解开发笔记
- 函数的重写和重载
- Page Object Model (Selenium, Python)(一)
- LoadRunner-Start Web Server端口被占用处理
- geotools创建线文件
- java并发包:概论
- 机器学习----集成学习(Boosting)
- Volley网络请求数据框架
- 项目管理中的拒绝技巧
- 解决类似 /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found 的问题
- mysql性能优化之EXPLAIN