2018年4月25日星期三

如何使用Selenium Web驱动程序执行拖放

我们已经看到了Selenium的行为课程和关于它的基础知识。如果你想 在这里阅读 有关更多详细信息。基本操作类用于执行各种鼠标和键盘驱动的动作,例如单击和保持,鼠标等。在此博客文章中,我们将在Selenium中使用Action类进行拖放操作。 

许多网站提供拖放,因此我们将看到我们如何自动化。

视频教程 -


对于演示目的,我正在使用样本拖放 -

//jqueryui.com/droppable/

让我们看看执行拖放的简单测试用例 -


import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class DragAndDrop {
 
ChromeDriver driver;
 
 @Before
 public  void setup(){
   
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  
      driver=new ChromeDriver();

       driver.manage().window().maximize();
  
        // start the application
  
       driver.get("//jqueryui.com/droppable/");
  
 }
 
 @Test
 public void testBootStrap() throws Exception
 {
 
  // Add 10 seconds wait
  Thread.sleep(10000);
   
  // Create object of actions class
  Actions act=new Actions(driver);
   
  driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
  // find element which we need to drag
  WebElement drag=driver.findElement(By.xpath("//*[@id='draggable']"));
   
  // find element which we need to drop
  WebElement drop=driver.findElement(By.xpath("//*[@id='droppable']"));
   
  // this will drag element to destination
  act.dragAndDrop(drag, drop).build().perform();
  
  // Add 2 seconds wait
  Thread.sleep(2000);
    }
 
@After
public void tearDown()
{ 
 driver.quit();
}


}

如何在Selenium Webdriver中处理Bootstrap下拉

在我们的 以前的POS我们已经看到了 如何在Selenium Webdriver中处理下拉列表 。在这篇文章中,我们将看到如何处理Bootsrap下拉列表。
引导下拉目和交互式下降,其使用列表动态定位和形成<ul> and <li> html tags.

下面是引导下拉列表的简单示例

//www.w3schools.com/bootstrap/bootstrap_dropdowns.asp

视频教程 -  
 

让我们看看Selenium代码样本 -

package test;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class BootstrapDropDown {
 
 ChromeDriver driver;
 
 @Before
 public  void setup(){
   
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  
      driver=new ChromeDriver();

       driver.manage().window().maximize();
  
        // start the application
  
       driver.get("//www.w3schools.com/bootstrap/bootstrap_dropdowns.asp");
  
 }
 
 @Test
 public void testBootStrap() throws Exception
 {
        // First we have to click on menu item then only dropdown items will display
  
       driver.findElement(By.xpath(".//*[@id='menu1']")).click();
  
  
        // adding 2 seconds wait to avoid Sync issue
  
        Thread.sleep(2000);
  
  
  
        // Dropdown items are coming in <a> tag so below xpath will get all
  
        // elements and findElements will return list of WebElements
  
        List<WebElement> list = driver.findElementsByXPath("//ul[@class='dropdown-menu test']//li/a");
  
  
  
        // We are using enhanced for loop to get the elements
  
        for (WebElement ele : list)
  
        {
  
           // for every elements it will print the name using innerHTML
  
           System.out.println("Values " + ele.getAttribute("innerHTML"));
  
  
  
           // Here we will verify if link (item) is equal to Java Script
  
           if (ele.getAttribute("innerHTML").contains("JavaScript")) {
  
              // if yes then click on link (iteam)
  
              ele.click();
  
  
  
              // break the loop or come out of loop
  
              break;
  
           }
  
        }
  
        // here you can write rest piece of code
  
    }
 
@After
public void tearDown()
{ 
 driver.quit();
}

}