如何添加使用Sel2Lib的python模块而不会出现多个关键字错误? [英] How to add a python module which uses Sel2Lib without getting multiple keyword error?

查看:89
本文介绍了如何添加使用Sel2Lib的python模块而不会出现多个关键字错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试导入一个python模块以骑3个多小时而没有成功.我按照此处第四个答案中说明的步骤进行操作,该步骤建议创建一个python模块Selenium2LibraryExt. 如何在机器人框架中获取所有文本?

I'm trying to import a python module to ride for more than 3 hours with no success. I went through the steps explained in fourth answer here where it suggests to create a python module Selenium2LibraryExt. How to get All Text in robot framework ?

我观察到的问题是,由于我现在在同一测试的其他代码中使用Selenim2Library,因此我导入了从Selenim2Library继承的Selenium2LibraryExt,因此我的测试不再知道例如Click Element关键字来自Selenim2Library或Selenium2LibraryExt,它给我多个关键字错误

the problem that i observe is that since i use Selenim2Library in my other codes of the same test now i import Selenium2LibraryExt which inherits from Selenim2Library, my test doesn't know anymore that e.g. Click Element keyword comesfrom Selenim2Library or Selenium2LibraryExt and it gives me multiple keyword error

所以我做到了 1-我删除了

So i did 1-I removed

       from Selenium2Library import Selenium2Library

从我的python模块的头开始,但我让它作为库保留在我的测试用例中:设置

from the head of my python module but i let it stay as a library in my test case: settings

 Library          Selenium2Library  

没有成功. 2-然后我删除了

it didn't work out. 2-Then i removed

Library          Selenium2Library  

来自我的测试用例,但添加了:

from my test case but added:

  from Selenium2Library import Selenium2Library  

在我的python模块的头部. 但是在两种情况下我都会出错.我如何不让我的测试看到2个selenium2library库?

in the head of my python module. but in both cases i get errors. how should i do not to have 2 selenium2library libraries seen by my test?

谢谢

推荐答案

如果使用继承的库,则测试数据需要导入Selenium2Library或自定义库,但不能同时导入两者.如果仅通过共享资源文件而不是直接在测试中导入,则更易于控制.

If you go with a library that inherits, then your test data needs to import either Selenium2Library or your custom library, but not both. If you only import through a shared resource file, and not directly in the tests, this is easier to control.

另一个选择是创建一个扩展Selenium2Library而不替换它的库:

Another option is to create a library that extends Selenium2Library without replacing it:

from robot.libraries.BuiltIn import BuiltIn

class Selenium2LibraryExt(object):

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

    def get_all_texts(self, locator):
        """Returns the text values of elements identified by `locator`."""
        elements = self._s2l._element_find(locator, False, True)
        return [e.text for e in elements]

如果您正在使用Selenium2Library的最新版本(> = 1.7),请获取Webelement和

If you are using a recent version of Selenium2Library (>= 1.7), Get Webelement and Get Webelements allow you to do a lot of things there are not keyword for...

@{texts}    Create List
@{elems}    Get Webelements    some locator
:FOR    ${elem}    IN    @{elems}
\    ${text}    Get Text    ${elem}
\    Append To List    ${texts}    ${text}

相同,但是使用扩展变量语法获取文本以与

Same thing but getting the text using the extended variable syntax to work with a webelement.

@{texts}    Create List
@{elems}    Get Webelements    some locator
:FOR    ${elem}    IN    @{elems}
\    Append To List    ${texts}    ${elem.text}

或者在Python中:

Or in Python:

from robot.libraries.BuiltIn import BuiltIn

class Selenium2LibraryExt(object):

    def get_all_texts(self, locator):
        """Returns the text values of elements identified by `locator`."""
        s2l = BuiltIn().get_library_instance('Selenium2Library')
        elements = s2l.get_webelements(locator)
        # or elements = BuiltIn().run_keyword('Get Webelements', locator)
        return [e.text for e in elements]

另请参见 https://stackoverflow.com/a/35323931/2532697

这篇关于如何添加使用Sel2Lib的python模块而不会出现多个关键字错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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