使用机器人框架进行网络自动化时如何从自定义库中实例化 webdriver 对象 [英] how to instantiate the webdriver object from the custom library when doing web automation using robot framework

查看:64
本文介绍了使用机器人框架进行网络自动化时如何从自定义库中实例化 webdriver 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在web自动化自定义库中定义用户关键字时,应该导入哪个库?selenium2library或从selenium导入webdriver.如何使用webdriver点击某些元素.请举例说明

while defining user keywords in custom library for web automation,which library should be imported?selenium2library or importing webdriver from selenium.How to use the webdriver to click on some elements.Kindly explain with an example

推荐答案

在大多数情况下,您不需要实例化 webdriver 对象.通常你使用 Selenium2Library 已经拥有的 webdriver 实例.您访问该实例的方式取决于您计划如何与 Selenium2Library 交互.请参阅扩展现有测试库"部分选项的用户指南.每个选项都有利有弊.

In most scenarios you do not need to instantiate the webdriver object. Usually you use the webdriver instance that Selenium2Library already has. How you access that instance depends on how you plan on interacting with Selenium2Library. See the "Extending existing test libraries" section in the user guide for options. Each options have pros and cons.

如果您继承 Selenium2Library,那么您将通过 self._current_browser() 访问驱动程序.

If you inherit Selenium2Library, then you would access the driver via self._current_browser().

如果您计划直接使用 Selenium2Library 而不是继承,您将同时声明 Selenium2Library 和您的自定义库.访问驱动程序的最方便的方法是通过私有属性,如下所示.

If you plan on using the Selenium2Library directly instead of inheriting, you would declare both Selenium2Library and your custom libraries. The most convenient way to access the driver is through a private property as demonstrated below.

from robot.libraries.BuiltIn import BuiltIn

class Selenium2LibraryExt(object):

    @property
    def _s2l(self):
        return BuiltIn().get_library_instance('Selenium2Library')

    @property
    def _driver(self):
        return self._s2l._current_browser()

    def perform_search(self, criteria):
        textbox = self._driver.find_element_by_name('q')
        textbox.send_keys(criteria)
        textbox.submit()

测试套件文件:

*** Settings ***
Test Teardown     Close All Browsers
Library           Selenium2Library
Library           c:/ws/Selenium2LibraryExt.py

*** Test Cases ***
Do a search
    Open Browser    http://www.google.com/    gc
    Perform Search    happiness

这篇关于使用机器人框架进行网络自动化时如何从自定义库中实例化 webdriver 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆