zoukankan      html  css  js  c++  java
  • selenium2.0处理case实例(二)

    本文通过具体代码处理过程, 来展示selenium中一些比较不常用的类的用法

           1、javascriptExcutor,通过将driver强转成JavascriptExecutor类型, 调用executeScript()就可执行javascript;

           2、TakesScreenshot,自动化测试中,截图可以帮助我们直观的定位错误、记录测试步骤;webdriver的截图功能十分方便,只需要driver.get到当前要截图的页面, 再将driver强转成TakesScreenshot 类型,调用getScreenshotAs(getScreenshotAs)方法得到截图文件

           3、DragAndDrop,模拟拖放操作, 使用actions.dragAndDrop(source, target).perform();来完成。source是要拖拉操作的对象,比如一张图片等, target是要放置的位置

           4、Select类,下拉框类型的控件,可以通过Select select=new Select(driver.findElement(By.id("select1"))); 来得到Select对象, 就可以对选项进行操作,比如,提供了“清空选择项”,“通过text/index/value来选择选项”,“获得所有的选项”,“获得当前选择的选项”等方法,具体可参见api,用法可参见博客“selenium2.0处理case实例(一)

    下面代码按照顺序,列出上述类具体使用过程和场景:

    package mavenSelenium;
    import java.io.File;
    import java.util.concurrent.TimeUnit;
    import org.junit.After;
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxProfile;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.openqa.selenium.interactions.Actions;
    import com.google.common.io.Files;
    
    public class TestBlog extends Assert {
        WebDriver driver;
        
        @Before
        public void init(){
            driver=new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        }
        
        @Test
        public void test()throws Exception{
            //1、driver  execute JavaScript
            driver.get("http://www.baidu.com");
            JavascriptExecutor js=(JavascriptExecutor)driver;
            String title=(String)js.executeScript("return document.title");
            System.out.println(title);
            
               //String script="function timeStamp(){ var d = new Date(); var m=d.getMonth()+1;var m=(m>9)?m:"0"+m; var dd=(d.getDate()>9)?d.getDate():"0"+d.getDate();var hh=(d.getHours()>9)?d.getHours():"0"+d.getHours();var mm=(d.getMinutes()>9)?d.getMinutes():"0"+d.getMinutes(); var ss=(d.getSeconds()>9)?d.getSeconds():"0"+d.getSeconds();var timeStamp=""+d.getFullYear()+m+dd+hh+mm+ss; return timeStamp; }var rs;rs=timeStamp();";
            String script="var d = new Date(); var m=d.getMonth()+1;var m=(m>9)?m:"0"+m; var dd=(d.getDate()>9)?d.getDate():"0"+d.getDate();     var hh=(d.getHours()>9)?d.getHours():"0"+d.getHours(); var mm=(d.getMinutes()>9)?d.getMinutes():"0"+d.getMinutes(); var ss=(d.getSeconds()>9)?d.getSeconds():"0"+d.getSeconds();var timeStamp=""+d.getFullYear()+m+dd+hh+mm+ss; return timeStamp;";
            System.out.println(script);
            String timestamps=(String)js.executeScript(script);
            System.out.println(timestamps);
            
            //2、TakesScreenshot
            File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            String descFilePath="d:\test2.jpeg";
            Files.copy(srcFile, new File(descFilePath));
            File descFile=new File(descFilePath);
            assertTrue(descFile.exists());
            
            //3、Drag And Drop
            driver.get("https://www.casa.com/login.qs?returnurl=/StyleBoard/Detail.qs?StyleBoardId=9468");
            driver.findElement(By.id("emailTextBox")).sendKeys("jennifer.huang@suryani.cn");
            driver.findElement(By.id("PwdTextBox_Security")).sendKeys("abc123");
            driver.findElement(By.id("SignInButton")).click();
            Thread.sleep(2000);
            WebElement source1=driver.findElement(By.xpath("//div[@id='SBProductBox1']/div/a/img"));
            WebElement target1=driver.findElement(By.xpath("//div[contains(@class,'SBDrag') and child::div[@id='SBProductBox2']]"));
            Actions actions=new Actions(driver);
            actions.dragAndDrop(source1, target1).perform();
            Thread.sleep(2000);
            WebElement source2=driver.findElement(By.xpath("//div[@id='SBProductBox2']/div/a/img"));
            WebElement target2=driver.findElement(By.xpath("//div[contains(@class,'SBDrag') and child::div[@id='SBProductBox1']]"));
            actions.dragAndDrop(source2, target2).perform();        
        }
        public void tearDown(){
            driver.quit();
        }
    
    }

    dragAndDropToObject(locatorOfObjectToBeDragged, locatorOfDragDestinationObject)

    dragAndDrop(locator, movementsString)  //movementsString - offset in pixels from the current location to which the element should be moved, e.g., "+70,-300"

    dragAndDrop(Point, Point) // Point point = driver.findElement(locator).getLocation();  get (x,y)

  • 相关阅读:
    leetcode_24. 两两交换链表中的节点
    Mysql大数据量分页优化
    MySQL 默认排序是什么
    spring注入map,spring注入一个接口的多个实现类在map里
    eureka缓存细节以及生产环境的最佳配置
    MySQL 5.7性能调优
    安装后的十个MySQL性能调整设置(版本:5.1、5.5 和5.6)
    docker部署tomcat应用和MySQL数据库
    MySQL热备工具Xtrabackup
    MySQL数据库的冷备方式
  • 原文地址:https://www.cnblogs.com/jenniferhuang/p/3482546.html
Copyright © 2011-2022 走看看