• 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();
        }
    }
  • 相关阅读:
    批量清理harbor镜像
    常用的git命令
    Gentoo网络管理方法总结
    Pelican主题配置:elegant
    Pelican搭建静态博客
    UNIX基础--安装应用程序: Packages 和 Ports
    UNIX基础--Manual Pages
    UNIX基础--Shells
    UNIX基础--进程和守护进程
    UNIX基础--磁盘组织
  • 原文地址:https://www.cnblogs.com/sylovezp/p/4201391.html
Copyright © 2020-2023  润新知