zoukankan      html  css  js  c++  java
  • selenium+junit4实现参数化自动化测试

    业务场景:在www.1905.com电影网中实现两个用户的登陆操作。

    代码如下:

    package com.m1905.junit;
    
    import java.util.Arrays;
    import java.util.Collection;
    
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    import org.junit.runners.Parameterized.Parameters;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebDriver.Navigation;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    @RunWith(Parameterized.class)
    public class LoginParameterTest {
        private static WebDriver driver;
        private static Navigation navigate;
        private static String url="http://www.1905.com";
        
        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
            driver = new FirefoxDriver();
            navigate = driver.navigate();
            navigate.to(url);
            driver.manage().window().maximize();
        }
        private String username;
        private String password;
        public LoginParameterTest(String username,String password){
            this.username = username;
            this.password = password;
        }
        @Parameters
        public static Collection<Object[]> prepareData(){
            Object[][] object = {{"user1","pwd1"},{"user2","pwd2"}};
            return Arrays.asList(object);
        }
        @Test
        public void testLogin() {
            try {
                Thread.sleep(8000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            WebElement LogAndReg = driver.findElement(By.xpath(".//*[@id='site_nav_md']/ul/li[2]/a"));
            LogAndReg.click();
            WebElement usernameBox = driver.findElement(By.xpath(".//*[@id='inputUsername']"));
            WebElement passwordBox = driver.findElement(By.xpath(".//*[@id='inputPassword']"));
            WebElement loginButton = driver.findElement(By.xpath(".//*[@id='loginreg']/div/div[1]/form/p/button"));
            usernameBox.clear();
            usernameBox.sendKeys(username);
            passwordBox.sendKeys(password);
            loginButton.click();
            WebDriverWait wait = new WebDriverWait(driver,10);
            WebElement logoutButton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='site_nav_md']/ul/li[3]/a[2]")));
            logoutButton.click();
        }    
        @AfterClass
        public static void tearDownAfterClass() throws Exception {
            driver.close();
        }
    }
  • 相关阅读:
    Python 通过最低位判断数字是偶数还是奇数
    C语言 windows下Ansi和UTF-8编码格式的转换
    C语言 Ubuntu系统 UTF-8 文字处理
    C语言 使用char字符实现汉字处理
    C# GUI应用 实现 2048游戏
    Docker部署Minio
    Ubuntu18.04开启root
    Ubuntu18.04安装Docker
    docker安装mysql
    idea启动项目端口被占用
  • 原文地址:https://www.cnblogs.com/sylovezp/p/4201391.html
Copyright © 2011-2022 走看看