使用Selenium测试

1、使用google浏览器,下载插件

2、下载Selenium Standalone Server包

3、录制

5、添加包

6、javacsv.jar

开始的时候导入的是opencsv.jar

7、修改代码进行批量处理

遇到的问题

使用chromedriver  要下载和chrome版本对应的chromedriver并将chromedriver.exe放到系统变量path可以找到的地方

package gtBailly;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Select;
import com.csvreader.CsvReader;

@RunWith(Parameterized.class)
public class Selenium {
    private WebDriver driver;
    private String baseUrl;
    private boolean acceptNextAlert = true;
    private StringBuffer verificationErrors = new StringBuffer();
    private String username,password,git;
    public Selenium(String username,String git){
        this.username=username;
        this.password=username.substring(4);
        this.git=git;
    }

    @Before
    public void setUp() throws Exception {
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Parameterized.Parameters
    public static Collection<Object[]> getData() throws IOException {
        Object[][] obj = new Object[97][];
        CsvReader reader = new CsvReader("C:\\Users\\GT\\Desktop\\input.csv", ',',
                Charset.forName("GBK"));
        int num = 0;
        reader.readHeaders();
        while(reader.readRecord()){
            obj[num] = new Object[]{reader.get(0), reader.get(1)};
            num++;
        }
        return Arrays.asList(obj);
    }

    @Test
    public void testSelenium() throws Exception {
        driver.get("https://psych.liebes.top/st/");
        driver.findElement(By.id("username")).clear();
        driver.findElement(By.id("username")).sendKeys(this.username);
        driver.findElement(By.id("password")).clear();
        driver.findElement(By.id("password")).sendKeys(this.password);
        driver.findElement(By.id("submitButton")).click();
        assertEquals(this.git, driver.findElement(By.xpath("/html/body/div/div[2]/a/p")).getText());
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    private boolean isAlertPresent() {
        try {
            driver.switchTo().alert();
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }

    private String closeAlertAndGetItsText() {
        try {
            Alert alert = driver.switchTo().alert();
            String alertText = alert.getText();
            if (acceptNextAlert) {
                alert.accept();
            } else {
                alert.dismiss();
            }
            return alertText;
        } finally {
            acceptNextAlert = true;
        }
    }
}

 

转载于:https://www.cnblogs.com/gtBailly/p/8809116.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值