zoukankan      html  css  js  c++  java
  • 【Web自动化测试——代码篇十】常用方法——调用JavaScript

      Java Python Ruby
    执行JavaScript代码 JavascriptExecutor jse= (JavascriptExecutor)driver;
    String jsContent= "JavaScript代码";
    jse.executeScript(jsContent);
    driver.execute_script(script, *args) driver.execute_script(script, *args)

    移动窗体滚动条

    window.scrollTo(左边距,右边距) 方法可以设置浏览器窗口滚动条的水平和垂直位置。

    Java

    package JavaTest;
    
    import java.io.IOException;
    import java.util.NoSuchElementException;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.Dimension;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class Test {
        public static void main(String[] arg) throws InterruptedException, IOException
        {
            WebDriver driver = new FirefoxDriver();
    
             // 设置隐示等待时长:10秒;
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("http://www.baidu.com");    
            
            try {
                //设置浏览器窗口大小
                driver.manage().window().setSize(new Dimension(500, 500));
    
                //window.scrollTo(左边距, 右边距)
                JavascriptExecutor jse = (JavascriptExecutor)driver;
                String jsContent = "window.scrollTo(10, 200)";
                jse.executeScript(jsContent);
            }
            catch(NoSuchElementException e)
            {
                System.out.println(e.getMessage());
            }
            finally
            {
                driver.close();
            }    
        }
    }

    Python

    from selenium import webdriver
    
    driver = webdriver.Firefox()
    
    # 隐式等待10S,打开网址
    driver.implicitly_wait(10)
    driver.get("http://www.baidu.com/")
    
    try:
        # 设置浏览器窗体大小
        driver.set_window_size(500, 500)
    
        # window.scrollTo(左边距, 右边距)
        js = "window.scrollTo(10,200)"
        driver.execute_script(js)
    except Exception as e:
        print(e.args[0])
    finally:
        driver.close()

    Ruby

    class Baidu
      require 'rubygems'
      require 'selenium-webdriver'
    
      # 打开firefox并输入网址
      driver = Selenium::WebDriver.for :firefox
    
      # 设置隐式等待时间10S
      driver.manage.timeouts.implicit_wait = 10
      driver.navigate.to "http://www.baidu.com"
    
      begin
        # 设置浏览器窗体大小
        driver.manage.window.resize_to(500, 500)
    
        # window.scrollTo(左边距, 右边距)
        js = "window.scrollTo(10, 200)"
        driver.execute_script(js)
      rescue => e
        puts e.message # 显示报错信息
      ensure
        driver.close
      end
    end

    图片.gif

    处理HTML5的视频播放

    大多数浏览器使用控件(如Flash)来播放视频。但是,不同的浏览器需要使用不同的插件。HTML5定义了一个新元素video,指定了一个标准方式来嵌套视频。
    JavaScript函数有个内置对象叫做arguments。argument对象包含了调用的参数数组,[0]表示取对象的第一个值
    current 返回当前视频/音频的URL。如果未设置,则返回空字符串。
    load()、play()、pause()等控制着视频的加载、播放和暂停。

    Java

    package JavaTest;
    
    import java.io.IOException;
    import java.util.NoSuchElementException;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class Test {
        public static void main(String[] arg) throws InterruptedException, IOException
        {
            WebDriver driver = new FirefoxDriver();
    
             // 设置隐示等待时长:10秒;
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("http://www.jq22.com/yanshi14795");    
            
            try {
                driver.switchTo().frame("iframe");
                WebElement video = driver.findElement(By.xpath("//*[@id='video1']"));
                
                JavascriptExecutor jse = (JavascriptExecutor)driver;
                //返回播放文件地址
                String url = jse.executeScript("return arguments[0].currentSrc", video).toString();
                System.out.println(url);
                
                jse.executeScript("return arguments[0].play()", video); //播放视频
                Thread.sleep(5000);
                jse.executeScript("return arguments[0].pause()", video); //暂停视频
            }
            catch(NoSuchElementException e)
            {
                System.out.println(e.getMessage());
            }
            finally
            {
                driver.close();
            }    
        }
    }

    Python

    from selenium import webdriver
    from time import *
    
    driver = webdriver.Firefox()
    
    # 隐式等待10S,打开网址(可直接通过frame的id和name定位)
    driver.implicitly_wait(10)
    driver.get("http://www.jq22.com/yanshi14795")
    
    try:
        driver.switch_to.frame("iframe")
        video = driver.find_element_by_xpath("//*[@id='video1']")
    
        # 返回播放文件地址
        url = driver.execute_script("return arguments[0].currentSrc;", video)
        print(url)
    
        driver.execute_script("return arguments[0].play()", video) # 播放视频
        sleep(5) # 播放5S
        driver.execute_script("return arguments[0].pause()", video) # 暂停视频
    except Exception as e:
        print(e.args[0])
    finally:
        driver.close()

    Ruby

    class Baidu
      require 'rubygems'
      require 'selenium-webdriver'
    
      # 打开firefox并输入网址
      driver = Selenium::WebDriver.for :firefox
    
      # 设置隐式等待时间10S
      driver.manage.timeouts.implicit_wait = 10
      driver.navigate.to "http://www.jq22.com/yanshi14795"
    
      begin
        driver.switch_to.frame('iframe')
        video = driver.find_element(:xpath => "//*[@id='video1']")
    
        # 返回播放文件地址
        url = driver.execute_script("return arguments[0].currentSrc;", video)
        puts url
    
        driver.execute_script("return arguments[0].play()", video) # 播放视频
        sleep(5)
        driver.execute_script("return arguments[0].pause()", video) # 暂停视频
      rescue => e
        puts e.message # 显示报错信息
      ensure
        driver.close
      end
    end

    图片.gif

  • 相关阅读:
    《JavaScript & jQuery交互式Web前端开发》之JavaScript基础指令
    hash_map原理及C++实现
    开源大数据引擎:Greenplum 数据库架构分析
    摆脱命令行,Ubuntu下配置Android开发环境
    JS学习十四天----server端运行JS代码
    【苦读官方文档】2.Android应用程序基本原理概述
    UVA
    android页面间传递对象
    大话设计模式C++版——建造者模式
    poj 3370 Halloween treats
  • 原文地址:https://www.cnblogs.com/CSgarcia/p/9548185.html
Copyright © 2011-2022 走看看