WebDriverIO实现UI自动化

WebDriverIO实现UI自动化

1.什么是webdriverIO
  • Github地址: https://github.com/webdriverio/webdriverio
  • 官网: https://webdriver.io/
2. 创建一个demo
  • 本地必须安装了node(node版本需要在 v12.16.1以上)

    使用 node -v 查看自己本地node版本

  • 初始化自己的代码

    $ mkdir webdriverio-demo && cd webdriverio-demo
    $ npm init -y
    
  • 安装 WebdriverIO CLI

    npm i --save-dev @wdio/cli
    
  • 生成配置文件wdio.conf.js

    npx wdio config -y
    

    wdio.conf.js配置文件用来安装所有必须的包文件

  • 创建 Spec 文件

    创建spec测试文件

    # mac/liunx
    $ mkdir -p ./test/specs
    # Windows
    $ mkdir -p ./test/specs
    
  • 打开文件目录查看文件结构

    项目.png

test下的js文件的官方的实例

  • 定义login page

    const Page = require('./page');
    
    /**
     * sub page containing specific selectors and methods for a specific page
     */
    class LoginPage extends Page {
        /**
         * define selectors using getter methods
         */
        get inputUsername () { return $('#username') }
        get inputPassword () { return $('#password') }
        get btnSubmit () { return $('button[type="submit"]') }
    
        /**
         * a method to encapsule automation code to interact with the page
         * e.g. to login using username and password
         */
        login (username, password) {
            this.inputUsername.setValue(username);
            this.inputPassword.setValue(password);
            this.btnSubmit.click(); 
        }
    
        /**
         * overwrite specifc options to adapt it to page object
         */
        open () {
            return super.open('login');
        }
    }
    
    module.exports = new LoginPage();
    
  • 第一个case,测试webdriver IO登录界面

    const LoginPage = require('../pageobjects/login.page');
    const SecurePage = require('../pageobjects/secure.page');
    
    describe('My Login application', () => {
        it('should login with valid credentials', () => {
            LoginPage.open();
    
            LoginPage.login('tomsmith', 'SuperSecretPassword!');
            expect(SecurePage.flashAlert).toBeExisting();
            expect(SecurePage.flashAlert).toHaveTextContaining(
                'You logged into a secure area!');
        });
    });
    
3. 运行case

使用下面命令来运行case

npx wdio wdio.conf.js

简化运行的命令,我们可以在package.json文件配置:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-K2Jb5Bcd-1608512024723)(https://i.loli.net/2020/12/20/FXy2JDCYGuZKt5V.jpg)]

然后用 npm run test来运行case就可以了,到此demo就完成了

使用docker运行case
# 下载selenium和zalenium的镜像
docker pull elgalu/selenuim
docker pull dosel/zalenium
# 将 wdio.conf.js配置文件中的services改成 [selenium standalone]
# 创建docker文件 --- docker-compose.yml
# 然后启动Selenium
docker-compose up
# 运行case:
npm run test
# 本地访问该端口,查看case运行时生成的录像
http://localhost:4444/dashboard/#
# 最后关闭Selenium
docker-compose down

docker-compose.yml

# Usage:
    #   docker-compose up --force-recreate
    version: ‘2.1’
    services:
      #--------------#
      selenium:
        image: elgalu/selenium
      zalenium:
        image: dosel/zalenium
        container_name: zalenium
        hostname: zalenium
        tty: true
        volumes:
          - /tmp/videos:/home/seluser/videos
          - /var/run/docker.sock:/var/run/docker.sock
        ports:
          - 4444:4444
        command: >
          start --desiredContainers 2
                --maxDockerSeleniumContainers 2
                --videoRecordingEnabled true
        depends_on:
          - selenium

最后附上demo的代码:

https://github.com/chirshee/webdriverio-demo
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
pytest是一个功能强大的Python测试框架,它可以用于各种类型测试,包括UI自动化测试。下面是使用pytest实现UI自动化测试的一般步骤: 1. 安装pytest:首先,你需要安装pytest库。可以使用pip命令进行安装:`pip install pytest` 2. 编写测试用例:使用pytest编写UI自动化测试用例。你可以创建一个Python文件,其中包含测试用例函数。每个测试用例函数应该以`test_`开头,并使用assert语句进行断言。 3. 配置pytest:创建一个名为`pytest.ini`的文件,用于配置pytest。在该文件中,你可以指定测试文件的匹配模式、报告生成方式等。 4. 运行测试:在命令行中运行pytest命令,它会自动查找并执行所有的测试用例。你可以使用`pytest`命令来运行所有的测试用例,或者使用`pytest <filename>`命令来运行特定的测试文件。 5. 查看测试结果:pytest会生成详细的测试报告,其中包含每个测试用例的执行结果、失败原因等信息。你可以在命令行中查看报告,也可以将报告保存为HTML格式。 这是一个简单的示例,演示了如何使用pytest进行UI自动化测试: ```python # test_example.py def test_login(): # 执行登录操作 # 断言登录成功后的页面是否正确显示 assert "Welcome" in driver.title def test_search(): # 执行搜索操作 # 断言搜索结果是否符合预期 assert len(driver.find_elements_by_class_name("result")) > 0 ``` 在命令行中运行`pytest`命令,pytest会自动查找并执行所有以`test_`开头的测试用例函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值