package com.dn.twohomework;

import java.util.ArrayList;

import java.util.Set;
import java.util.List;
// import org.apache.bcel.generic.Select;
 //import org.openqa.selenium.support.ui.Select;
//import org.apache.bcel.generic.Select;
 //import com.sun.org.apache.bcel.internal.generic.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

import java.util.Iterator;

import com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput;
//import com.gargoylesoftware.htmlunit.javascript.host.Iterator;
//import com.gargoylesoftware.htmlunit.javascript.host.Set;
//import com.sun.media.sound.FFT;
//import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
import java.util.HashSet;;

public class keyWord1 {
    private static WebDriver driver;

    // 打开浏览�?
    public void openBrowser(String b, String xpath) {
        if ("cc".equals(b)) {
            
            ChromeDriver chrome = new ChromeDriver("webdriver.chrome.driver", xpath);
            driver = chrome.getdriver();

        } else if ("ff".equals(xpath)) {
            FFDriver ff = new FFDriver("webdriver.gecko.driver", xpath);
            ff.getdriver();
            
        } else if ("ie".equals(b)) {

            IEDriver ie = new IEDriver("webdriver.ie.driver", xpath);
            driver = ie.getdriver();
        }
    }

    public static WebDriver getDriver() {
        return driver;
    }

    public static void setDriver(WebDriver driver) {
        keyWord1.driver = driver;
    }

    // 输入网址
    public void geturl(String url) {
        try {
            driver.get(url);
            

             //ChromeOptions options = new ChromeOptions();
            //options.addArguments("--start-fullscreen");
            //driver.manage().window().maximize();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("打开浏览器失�?");
            e.printStackTrace();
        }
    }

    // 等待时间
    public void sleep(String time) throws NumberFormatException, InterruptedException {

        Thread.sleep(Integer.parseInt(time));
    }

    // 输入内容
    public void input(String xpath, String word) {
        try {
            driver.findElement(By.xpath(xpath)).clear();
            driver.findElement(By.xpath(xpath)).sendKeys(word);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("输入字符串失�?");
            e.printStackTrace();
        }
    }

    // 点击
    public void click(String xpath) {
        try {
            driver.findElement(By.xpath(xpath)).click();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("点击失败");
            e.printStackTrace();
        }

    }

    // 关闭旧浏览器,切换到新窗�?
    public void closeOldWin() {
        List<String> handle = new ArrayList<String>();
        Set<String> s = driver.getWindowHandles();
        // 循环获取数组里的句柄,保存到链表handle里面
        for (Iterator<String> it = s.iterator(); it.hasNext();) {
            handle.add(it.next().toString());

        }

        driver.close();
        driver.switchTo().window(handle.get(1));

    }

    // 关闭新浏览器,切换到旧窗�?
    public void closeNewWin() {
        List<String> handle = new ArrayList<String>();
        Set<String> s = driver.getWindowHandles();
        // 循环获取数组里的句柄,保存到链表handle里面
        for (Iterator<String> it = s.iterator(); it.hasNext();) {
            handle.add(it.next().toString());

        }
        driver.switchTo().window(handle.get(1));

        driver.close();
        driver.switchTo().window(handle.get(0));

    }

    // 进入子页�?
    public void intoIframe(String xpath) {
        try {
            driver.switchTo().frame(driver.findElement(By.xpath(xpath)));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("进入Iframe失败");
            e.printStackTrace();
        }

    }

    // 关闭子页�?
    public void outIframe() {
        driver.switchTo().defaultContent();
    }

    // 获取文字信息
    public void getText(String xpath) {
        System.out.println(driver.findElement(By.xpath(xpath)).getText());
    }

    // hover
    public void hover(String xpath){
        try {
            Actions builder = new Actions(driver);
            builder.moveToElement(driver.findElement(By.xpath(xpath))).build().perform();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            
            e.printStackTrace();
            System.out.println("悬停失败");
        }
    }
    //获取链接link
    public String getLink(String xpath){
        String link = "about:bliank";
        try {
            link = driver.findElement(By.xpath(xpath)).getAttribute("href");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            
            e.printStackTrace();
            System.out.println("获取链接失败");
        }
        return link;
    }
    //提交表单
    public void submit(String xpath){
        try {
            driver.findElement(By.xpath(xpath)).submit();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("提交失败");
            e.printStackTrace();
        }
    }
    //选择下拉框
    public void select(int num,String xpath){
   Select sel = new Select(driver.findElement(By.xpath(xpath)));
   sel.selectByIndex(num);

    }
    //通过js操作
    public void exejs(String text){
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript(text);
    }
    //上传图片
    public void uploadpiture(String name,String path){
        try {
            driver.findElement(By.name(name)).sendKeys(path);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("上传失败");
            
        }
    }
    //获得元素属性的值
    public String GetsTheValueOfTheElement(String xpath,String element){
    try {
        String getvalues =    driver.findElement(By.xpath(xpath)).getAttribute(element);
        return getvalues;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("获取元素属性的值失败");
        return null;
    }
    }
    //点击alert的确认按钮
        public void clickAlert(){
            driver.switchTo().alert().accept();;
        }
}

selenium--关键字驱动的更多相关文章

  1. Selenium关键字驱动测试框架Demo(Java版)

    Selenium关键字驱动测试框架Demo(Java版)http://www.docin.com/p-803493675.html

  2. UI自动化测试框架之Selenium关键字驱动

    一.原理及特点 1. 关键字驱动测试是数据驱动测试的一种改进类型 2. 主要关键字包括三类:被操作对象(Item).操作(Operation)和值(value),用面向对象形式可将其表现为Item.O ...

  3. UI自动化测试框架之Selenium关键字驱动 (转)

    摘要 自动化测试框架demo,用关键字的形式将测试逻辑封装在数据文件中,测试工具解释这些关键字即可对其应用自动化 一.原理及特点 1.   关键字驱动测试是数据驱动测试的一种改进类型 2.    主要 ...

  4. 【转】UI自动化测试框架之Selenium关键字驱动

    原网址:https://my.oschina.net/hellotest/blog/531932#comment-list 摘要: 自动化测试框架demo,用关键字的形式将测试逻辑封装在数据文件中,测 ...

  5. Python&Selenium 关键字驱动测试框架之数据文件解析

    摘要:在关键字驱动测试框架中,除了PO模式以及一些常规Action的封装外,一个很重要的内容就是读写EXCEL,在团队中如何让不会写代码的人也可以进行自动化测试? 我们可以将自动化测试用例按一定的规格 ...

  6. selenium 关键字驱动部分设计思路

    1 说明: 1.以下的代码亲测直接可用, 2.设计思路来自博客园的 张飞_ :http://www.cnblogs.com/zhangfei/p/5330994.html   /   http://w ...

  7. 数据驱动 vs 关键字驱动:对搭建UI自动化测试框架的探索

    UI自动化测试用例剖析 让我们先从分析一端自动化测试案例的代码开始我们的旅程.以下是我之前写的一个自动化测试的小Demo.这个Demo基于Selenium与Java.由于现在Selenium在自动化测 ...

  8. Appium 自动化测试框架:关键字驱动+数据驱动

    1. 关键字驱动框架简介 2. 框架结构说明 3. 框架代码实现 action 包  page_action.py business_process 包 case_process.py data_so ...

  9. UI自动化测试框架:关键字驱动+数据驱动

    1. 关键字驱动框架简介 2. 工程结构说明 3. 工程代码实现 action 包  page_action.py business_process 包 case_process.py data_so ...

  10. java自动化-关键字驱动在junit接口自动化的应用

    本文是继承上文的基础上进行的讨论,建议读者先阅读http://www.cnblogs.com/xuezhezlr/p/9097137.html和https://www.cnblogs.com/xuez ...

随机推荐

  1. Hibernate错误:Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update

    报错:Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execu ...

  2. 使用Cain&Abel进行网络嗅探

    启动Cain&Abel并切换到"sniffer(嗅探)"标签. 第一步:需要对Cain&Abel进行配置,先单击最上面的"Configure(配置)&qu ...

  3. 恐怖的ifdown eth0;0

    下午闲的蛋疼,随手给测试机配了个浮动地址eth0:0. ping了下OK,内网访问没问题. 准备收手的时候,瞄了一眼ifcfg-eth0:0的配置,发现广播地址BROADCAST写成了BOADCAST ...

  4. JQuery EasyUI的常用组件

    jQuery EasyUI 是一个基于 jQuery 的框架,集成了各种用户界面插件,该框架提供了创建网页所需的一切,帮助您轻松建立站点. 注:本次介绍的JQuery EasyUI版本为1.5版. 一 ...

  5. CSS 回流(reflow)

    摘录: 1. 回流 回流是指浏览器为了重新渲染部分或者全部的文档而重新计算文档中元素的位置和几何构造的过程.    因为回流可能导致整个dom树的重新构造,所以会影响性能. 2. display:no ...

  6. 使用JS开发桌面端应用程序NW.js-3-开发问题小记

    前言 因为我们的项目是2C的,而XP系统是最大的用户量占比,所以只能使用nw开发而不能用Electron,本文谨记开发nw过程中遇到的各种问题以及解决方案. nw.Window.open打开新窗口不能 ...

  7. java设计模式 01 开山篇

    一. 设计模式概述 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. ...

  8. 如何快速轻松学习bootstrap

    我以前也是通过看一些视频教程来学的,比如慕课网上的,比如51cto上的那些零基础入门bootstrap什么的,还有一些培训班里流传出来的,感觉晕乎乎的,不知所云. 还是在后面不断使用的过程中慢慢体会到 ...

  9. WEB安全:文件上传漏洞

    文件上传漏洞过程 用户上传了一个可执行的脚本文件,并通过此脚本文件获得了执行服务器端命令的能力. 一般的情况有: 上传文件WEB脚本语言,服务器的WEB容器解释并执行了用户上传的脚本,导致代码执行: ...

  10. jmeter问题处理随笔1 - CSV取值数据异常处理(包含"号,","号的情况)

    背景 jmeter测试中通过CSV进行用例数据的管理,在result断言中间需要使用json格式的数据,会包含 " ",",这个时候发现CSV取值会报错或者乱码 解决 用 ...