`
cocoIT
  • 浏览: 51023 次
  • 性别: Icon_minigender_1
  • 来自: 福建
文章分类
社区版块
存档分类
最新评论

Introducing the Selenium-WebDriver API by Example

 
阅读更多

WebDriver is a tool for automating web application testing, and in particularto verify that they work as expected. It aims to provide a friendly API that’seasy to explore and understand, easier to use than the Selenium-RC (1.0) API,which will help to make your tests easier toread and maintain. It’s not tied to any particular test framework, so it canbe used equally well in a unit testing or from a plain old “main” method.This section introduces WebDriver’s API and helps get you started becomingfamiliar with it. Start by setting up a WebDriver project if you haven’t already.This was described in the previous section,Setting Up a Selenium-WebDriver Project.

Once your project is set up, you can see that WebDriver acts just as any normal library:it is entirely self-contained, and you usually don’t need to remember to start anyadditional processes or run any installers before using it, as opposed to the proxy serverwith Selenium-RC.

Note: additional steps are required to use Chrome Driver, Opera Driver, Android Driverand iOS Driver

You’re now ready to write some code. An easy way to get started is thisexample, which searches for the term “Cheese” on Google and then outputs theresult page’s title to the console.

package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium2Example  {
    public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new FirefoxDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        
        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());
        
        //Close the browser
        driver.quit();
    }
}

In upcoming sections, you will learn more about how to use WebDriver for thingssuch as navigating forward and backward in your browser’s history, and how totest web sites that use frames and windows. We also provide a morethorough discussions and examples.

分享到:
评论

相关推荐

    selenium webdriver 3 practical guide 第二版

    Selenium WebDriver 3 Practical Guide will walk you through the various APIs of Selenium WebDriver, which are used in automation tests, followed by a discussion of the various WebDriver implementations...

    Forrester-2021_01-Introducing-The-Zero-Trust-Edge-Model

    Forrester-2021_01-Introducing-The-Zero-Trust-Edge-Model

    Introducing-Bluetooth-LE-Audio-book

    《蓝牙LE音频指南》这本书由蓝牙SIG组织的成员Nick Hunn撰写,主要介绍了最新的蓝牙LE音频规范及其将如何改变我们设计和使用音频及电话产品的方式。蓝牙LE(Low Energy)音频是蓝牙技术的一个重要更新,它旨在提高...

    introducing-the-amba-coherent-hub-interface-102407-0100-01-en

    《AMBA 同步Hub接口简介》 AMBA(Advanced Microcontroller Bus Architecture)是由ARM公司开发的一种开放标准的片上系统(SoC)互连架构,用于连接微处理器、存储器和其他外设。AMBA Coherent Hub Interface是AMBA...

    LP introducing-hong-kong

    LP introducing-hong-kong

    C++标准库 Introducing-The-STL

    My intent here is to provide a gentle introduction to some of the container classes definedbytheSTL Mypersonalexperienceisthatthepair thevectorandthemap are used more often than the other STL classes ...

    selenium framework design data driven testing

    The Selenium WebDriver 3.x Technology is an open source API available to test both Browser and Mobile applications. It is completely platform independent in that tests built for one browser or mobile ...

    Packt.Learn.Selenium.rar

    You'll design and build a Selenium grid from scratch to enable the framework to scale and support different browsers, mobile devices, and platforms.You'll strategize and handle a rich web UI using the...

    Introducing InnoDB Cluster--2018

    Discover the newest high-availability features in MySQL Set up and use InnoDB Cluster as an HA solution Migrate your existing servers to MySQL 8 Employ best practices for using InnoDB Cluster ...

    introducing-java-8.pdf

    Java 8 是Java编程语言的一个重要更新版本,由Raoul-Gabriel Urma所著的《Introducing Java 8》是一本介绍Java 8新特性的指南书。本书提供了Java 8中新增的Lambda表达式和流(Streams)的快速入门,旨在帮助程序员...

    introducing-python-master_python教程_pet4eq_

    "introducing-python-master"这个教程是为那些想要入门Python编程的初学者设计的,它将带领你逐步了解Python的基础知识。 一、Python简介 Python是由Guido van Rossum在1991年开发的,它的设计哲学强调代码的可读性...

    Introducing+Feature-Wise+Multiplication+to+CTR+Ranking+Models

    《特征向量乘法在CTR排名模型中的应用——以实例引导的MaskNet》 ...传统的CTR模型通常基于特征的线性组合进行预测,例如逻辑回归(Logistic Regression)或因子分解机(Factorization Machines)。...

    Introducing-Python-Modern-Computing-in-Simple-Packages.pdf.pdf

    根据提供的文件信息,以下是对文档《Introducing Python: Modern Computing in Simple Packages》的知识点的详细说明: 1. 标题解释: 《Introducing Python: Modern Computing in Simple Packages》是一本关于...

    Introducing Data Science - Big Data, Machine Learning and more, using Python

    《Introducing Data Science - Big Data, Machine Learning and more, using Python》这本书是对数据科学领域中的大数据、机器学习以及其他相关知识点使用Python工具进行讲解的入门教材。作者Davy Cielen、Arnold B....

    Introducing-Small-Basic.pdf

    Small-Basic 儿童编程中文教程,Small Basic 是一门针对初学者设计的使编程变得非常容易,亲切,有趣的编程语言。Small Basic 的目的 在于消除障碍,充当通往令人惊奇的计算机编程世界的踏脚石。

    Flink-Forward-SF-2017_Eron-Wright_Introducing-Flink-TensorFlow

    Apache Flink与TensorFlow的结合是在大数据处理和机器学习领域中一项具有重要意义的技术。首先,我们需要理解Flink与TensorFlow各自的定位和功能。 Apache Flink 是一个开源的流处理框架,能够处理高吞吐量的数据流...

    英文版Introducing iOS 8 - Swift Programming from Idea to App Store

    这本书名为《Introducing iOS 8 - Swift Programming from Idea to App Store》,是在iOS 8发布后不久推出的一本旨在引导初学者学习如何从零开始开发iOS应用的指南。本书由Steve Derico撰写,他不仅是BixbyApps的...

Global site tag (gtag.js) - Google Analytics