UI Automation: How to improve the portability of automated frameworks (eliminate absolute paths)

Source: Internet
Author: User

Background:
Bloggers have just entered the UI Automation test for about 2 months, through self-study set up the first automated testing framework, with the depth of learning, found a framework for a lot of optimization points, just take advantage of the project site to test their own reconstruction, and their own test framework for the two-phase refactoring, One of the key points of refactoring is how to remove the absolute path used or configured in the source code, improve the portability of the framework, and ensure that other students get their own framework. You can use it without modifying the absolute path in the configuration file or code.
In the process of refactoring, encountered some problems, also take out to share with you. Initial scenario: Set up a py file read root (failed)

The first blogger set up a Python file to read the project's root directory, and then run found the path to get no problem, feel flattered, and then out of the question, when other classes call the class in this file to get the root directory, found not to get the root directory. As an example:

autotest/getrootpath.py
Autotest/config/chromedriver.exe
autotest/testcase/test_login.py
autotest/run_all.py
Description: BasePage Class I defined the path of the Chromedriver (under the engineering config)

The above is a simple project structure where the project name defines a class and a method in autotest,getrootpath.py, and a core approach is the OS.GETCWD () in the OS module, which gets the path to the folder where the current file resides. Including the system of what C disk D disk and so on.
Then in the run_all.py I test the login method just encapsulated, found that there is no problem running, and then I am happy to testcase write a test_login test case, the results of the following error:

Error message: "' chromedriver ' executable needs to is available in ' path"

I believe that this sentence should not be unfamiliar to you, why the same code will be an error, young himself a face, and then Baidu method, use what Os.environ () to add chromedriver to environment variables. Then or GG, can only locate the reason, through the print Chromedriver access path, found the problem.
C:\users\pycharmprojects\webdriveautotest-v2**testcase**\config\chromedriver.exe
C:\Users\PycharmProjects\webDriveAutoTest-V2\config\chromedriver.exe
Description: By stitching the root directory and the specified file path, the source code is as follows

Chrome_driver_path = RootPath +r ' \config\chromedriver.exe '

The cause of this problem is believed to have been understood, because the script file running in the TestCase directory is centered on the current directory, and when the call to basepage the root directory path plus testcase, also under the project Run_ All.py gets the real root directory so his run is OK.

the solution to this failure scenario is to see the next blog post for the blogger. Stupid Way: string fragmentation + stitching (success)

    def getrootpath (self):
        #获取当前脚本路径
        initpath = OS.GETCWD ()
        #print (initpath)
        #查找初始路径中特定尾部子串的位置
        index = initpath.find (' V2 ')
        #分片, get root directory
        RootPath = initpath[:index+2]
        #返回 return
        RootPath

One small way to do this is to pull back the root of the blogger, the idea is relatively simple, pipe him what path, first get the current path, anyway, the project name is fixed, then find a project path of the tail string positioning, such as the project name "Autotest-v2", So I look for the V2 position from the initial path I get, and I get the path in front of V2.
In this case, you can avoid any of the C disk D disks when you use a file under the project in other files.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.