Selenium入门

4 篇文章 0 订阅
2 篇文章 0 订阅

最近测试中用到了Selenium,我感觉到非常不合适,因为,EasyUI使用的是动态ID,而Selenium需要根据Xpath或者id找对应的页面元素,用EasyUI就表示,大部分情况下,你都是不能用id得到的,只能通过xpath。虽然出现了很多问题,不过还是写完了。废话不多说了,开始主题。
首先 Jar包和chromeDriver,IEDriver,这里, 密码:fw0p
selenium是非常强大的功能测试工具,可以自动在页面中执行,可以用来抓取网页的内容。和Python配合非常给力,这里是Java
一下为最简单的一个项目代码

public abstract class SeleniumTest{
    此处可以定义常量
    /**
     * 浏览器驱动
     */
//chrome********
System.setProperty("webdriver.chrome.driver", "D:\\devlope\\chromedriver.exe");
WebDriver driver =new ChromeDriver();
//firefox******
//FirefoxBinary firefoxBinary = new FirefoxBinary(new File("D:\\Firefox\\Mozilla Firefox\\firefox.exe"));
                FirefoxBinary firefoxBinary = new FirefoxBinary(new File("D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"));
FirefoxProfile firefoxProfile = new FirefoxProfile();
WebDriver driver = FirefoxDriver(firefoxBinary,firefoxProfile);
//ie***************
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
WebDriver driver = InternetExplorerDriver(ieCapabilities);

public static void before() {
        // TODO Auto-generated method stub
    }
    @Test
    public abstract void clickStart();
    public static void after() {
        // TODO Auto-generated method stub
    }
}

然后一个类继承抽象类

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class deal extends SeleniumTest{
    @BeforeClass
    public static void before() {
        // TODO before
        before();
    }
    @Override
    public void clickStart() {
        // TODO 测试方法
    }
    @AfterClass
    public static void afterClass() {
        // TODO After
        after();
    }
}

除了这些之外还有几个方法在easy和selenium中经常用到

1、. 鼠标双击:
2、. 最大化:webDriver.manage().window().maximize();
3、. 基本操作:content.findElement(By.xpath(“xpath路径”)).click();
4、. 更换frame:content.switchTo().frame(“frame名字”);
5、. 弹出框确认:content.switchTo().alert().accept();
6、. 鼠标双击和移动进度条:

Actions action = new Actions(driver);
        action.doubleClick(
        content.findElement(By.xpath("")));
        Thread.sleep(1000);
        action.moveToElement(
                content.findElement(By.xpath("")))
                .perform();

7、. 关闭easyUi中打开的子窗口

    // 关闭之前打开的窗口
    public void closeWindows() {
        driver.switchTo().defaultContent();
        driver.switchTo().frame("frame");
        content.findElement(By.xpath(""))
                .click();
    }

8、. 设置为默认frame,跳到最外层

    public void backFrame(String xpath) {
        // TODO 返回最开始的frame,在此过程中标签栏不关闭,所以不用重新打开。
        driver.switchTo().defaultContent();
        driver.switchTo().frame("frame");
        Utils.setWaitTime(1);
        // 再次点击前期费用编辑
        content.findElement(By.xpath(xpath)).click();
        Thread.sleep(1000);
    }

9、. 关闭浏览器:driver.close();

问题与使用
首先,如果 no element,有两个办法,
1、在firefox中用firebug取xpath,不用chrome的相对路径
2、设置延时,selenium执行的很快,真的很快,所以如果你的弹出框没出来的时候,就会报no element的错误了
还有一个错误
java.lang.StackOverflowError
这个是因为,我的三个类相互调用了,所以每个引用都不指向空,最后导致溢出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值