java+selenium+testNG自动化测试实践
第一步,本地搭建web服务
我使用开源JPRESS项目在本地搭建web服务,用于测试。关于JPress网上搜一下就可以下载源码,根据指导文档搭建web服务即可。
第二步 注册云码,用于打码解析Jpress后台登录界面验证码解析,扫微信即可享受500免费积分使用
第三部 搭建本地测试项目框架
Java + Selenium + TestNG + Rest Assured
gitee: https://gitee.com/the-wind-of-that-year/jpress_web-ui_-test.git
登录测试用例
package jpress;
import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import demo.SetupEnvironment;
import org.openqa.selenium.*;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import utility.YunMaServer;
import java.io.IOException;
public class TestLogin {
private Logger logger = LoggerFactory.getLogger(TestLogin.class);
private WebDriver driver;
@BeforeClass
public void setup(){
driver = SetupEnvironment.getDriver();
driver.manage().window().maximize();
}
@Test
@Parameters({"userName", "passWord"})
public void loginTest(String userName, String passWord) throws IOException {
driver.get("http://localhost:8080/admin/login");
WebElement user = driver.findElement(By.name("user"));
user.clear();
user.sendKeys(userName);
WebElement pwd = driver.findElement(By.name("pwd"));
pwd.clear();
pwd.sendKeys(passWord);
WebElement captcha
= driver.findElement(By.id("captcha"));
WebElement element
= driver.findElement(By.id("captcha-img"));
Rectangle rect =
element.getRect();
String screenshotAs
= element.getScreenshotAs(OutputType.BASE64);
logger.warn(screenshotAs);
String imageCode =
YunMaServer.getImageCode(screenshotAs);
captcha.clear();
captcha.sendKeys(imageCode);
driver.findElement(By.cssSelector("button.btn-primary")).click();
WebElement element1
= driver.findElement(By.cssSelector(".main-header li.dropdown
a.nav-link"));
String name =
element1.getText();
Assert.assertEquals(name, userName);
}
}