哈哈哈哈哈哈哈,记录下,我的第一个自动化测试研究成果.........可能对测试大佬来说so easy ,但只有java开发经验的我还是超激动的......
目标:自动输入账户、密码、自动滑动滑块通过验证,点击登录按钮登录系统。如图所示:
嘻嘻.....话不多说,上代码:
package com.test.Selenium_Lesson;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class LoginTest {
public static void main(String[] args) throws InterruptedException {
Thread.sleep(3000);
System.setProperty(
"webdriver.chrome.driver",
"C://Users//Administrator//AppData//Local//Google//Chrome//Application//chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
// 与浏览器同步非常重要,必须等待浏览器加载完毕
webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// 打开目标地址
webDriver.get("http://127.0.0.1:8080");
Thread.sleep(1000);
// 输入账号 密码并登陆系统
webDriver.findElement(By.name("username")).sendKeys("lpq");
webDriver.findElement(By.name("password")).sendKeys("000000");
// 滑动,把滑块从左端移到右端
// 新建Actions类
Actions action = new Actions(webDriver);
// 查找可拖拽元素
WebElement background = webDriver.findElement(By
.cssSelector("#verify-wrap > span.fix-tips.fixTips"));
//获取滑块的长度
Dimension span_background_size = background.getSize();
//获取滑块的位置
WebElement button = webDriver.findElement(By
.cssSelector("#verify-wrap > span.drag-btn.dragBtn"));
Point button_location = button.getLocation();
System.out.println("滑块的位置:" + button_location);
//计算滑动移动的目标位置,把滑块从左端移到右端
int x_location = button_location.x + span_background_size.width;
int y_location = button_location.y;
action.dragAndDropBy(button, x_location, y_location).perform();
// 获取登录按钮
WebElement loginButton = webDriver.findElement(By
.xpath("//*[@id=\"loginForm\"]/div[5]/button"));
// 点击登录
loginButton.click();
// 暂停五秒钟后关闭
// Thread.sleep(5000);
// webDriver.quit();
}
}
定位元素如图所示:
webdriver模拟鼠标拖动验证码,这个费力老长时间研究,感谢python的启发,计算坐标确定移动位置.....