selenium + java patterning process on the slider (1)

New novice, the initial contact with selenium + Java automated testing, tried to share what point learning.

Doing the automation of time, sometimes we encounter graphical verification problem, especially now that most sites have added a slider to check today to share with you the easiest handling slider verification;

 

This process step slider:

1. The first positioning element and slider control;

2. slider sliding distance is obtained, i.e. the destination coordinates of the slider;

3. Drag the slider.

1. The slider positioning control, as shown below

 Outside the control of the positioning line, positioned inside the small one will do

//
WebElement sour = driver.findElement(By.cssSelector(".cpt-img-double-right"));

//
WebElement sour = driver.findElement(By.cssSelector(".cpt-drop-btn"));

2. slider sliding distance is obtained, i.e. the destination coordinates of the slider, as in FIG.

 

Go movement of the slider is the position of point B or point C from point A, the need to coordinate B or C, can be obtained,

A point to the origin, the horizontal distance X, the vertical distance Y

1 //整个拖拽框的控件元素
2     WebElement ele = driver.findElement(By.cssSelector(".cpt-bg-bar"));
3 //拖拽的宽度即x的距离
4     int x = ele.getSize().getWidth();
5 //拖拽的高度即y的距离
6     int y = ele.getSize().getHeight();
7             

3.拖动滑块

1 //拖拽的动作
2     Actions action = new Actions(driver);
3     action.dragAndDropBy(sour, x, y).perform();

附上完整代码

 1 package se_2019;
 2 
 3 import org.openqa.selenium.By;
 4 import org.openqa.selenium.WebDriver;
 5 import org.openqa.selenium.WebElement;
 6 import org.openqa.selenium.firefox.FirefoxDriver;
 7 import org.openqa.selenium.interactions.Actions;
 8 public class lianxi_191001 {
 9 
10     public static void main(String[] args) throws InterruptedException {
11         //建立驱动
12             System.setProperty("webdriver.gecko.driver", "C:\\Program Files\\Mozilla Firefox\\geckodriver.exe");
13             WebDriver driver = new FirefoxDriver();
14         //输入网址
15             driver.get("https://passport.ctrip.com/user/login");
16         //输入账号密码并点击登录
17             driver.findElement(By.id("nloginname")).sendKeys("18519523213");
18             driver.findElement(By.id("npwd")).sendKeys("1");
19             driver.findElement(By.id("nsubmit")).click();
20             Thread.sleep(3000);
21         //滑块控件元素
22             //WebElement sour = driver.findElement(By.cssSelector(".cpt-img-double-right"));
23             WebElement sour = driver.findElement(By.cssSelector(".cpt-drop-btn"));
24         //整个拖拽框的控件元素
25             WebElement ele = driver.findElement(By.cssSelector(".cpt-bg-bar"));
26         //拖拽的宽度即x的距离
27             int x = ele.getSize().getWidth();
28             System.out.println(ele.getSize().getWidth());
29         //拖拽的高度即y的距离
30             int y = ele.getSize().getHeight();
31             System.out.println(ele.getSize().getHeight());
32             Thread.sleep(3000);
33         //拖拽的动作
34             Actions action = new Actions(driver);
35             action.dragAndDropBy(sour, x, y).perform();
36             Thread.sleep(2000);
37         //关闭窗口
38             driver.close();
39         
40     }
41 
42 }

 

Guess you like

Origin www.cnblogs.com/zctyk/p/11615646.html