2023年7月9日发(作者:)
SeleniumLibrary4.5.0关键字详解(三)SeleniumLibrary4.5.0 关键字详解(三)库版本:4.5.0库范围:全局命名参数:受⽀持简介SeleniumLibrary是Robot Framework的Web测试库。本⽂档说明了如何使⽤SeleniumLibrary提供的关键字。 有关安装,⽀持等信息,请参见 python3.9.0 + robotframework +selenium3 实例体验。SeleniumLibrary在内部使⽤Selenium WebDriver模块来控制Web浏览器。 有关常规Selenium的更多信息,请参见Selenium。内容列表1、元素定位2、浏览器和窗⼝3、超时,等待和延迟4、运⾏故障功能5、布尔参数6、webDriver事件7、线程⽀持8、插件9、引⼊11、关键字超时、等待和延迟本节讨论了如何等待元素出现在⽹页上以及以其他⽅式降低执⾏速度的不同⽅式。它还说明了设置各种超时,等待和延迟时可以使⽤的时间格式。超时(timeout)SeleniumLibrary包含各种关键字,这些关键字具有可选的超时参数,该参数指定这些关键字应等待某些事件或操作的时间。这些关键字包括例如Wait …关键字和与告警相关的关键字。另外,执⾏异步Javascript。尽管它没有超时参数,但它使⽤超时来定义异步JavaScript可以运⾏多长时间。这些关键字使⽤的默认超时可以通过使⽤Set Selenium Timeout关键字来全局设置,也可以在导⼊库时与timeout参数⼀起设置。有关⽀持的超时语法,请参见下⾯的时间格式。隐式等待(Implicit wait)隐式等待指定搜索元素时Selenium最多等待多长时间。导⼊库时,可以使⽤Set Selenium Implicit Wait关键字来设置,也可以使⽤implicit_wait参数来设置。有关此功能的更多信息,请参见Selenium⽂档。有关⽀持的语法,请参见下⾯的时间格式。selenium执⾏速度可以通过使⽤Set Selenium speed关键字全局降低Selenium的执⾏速度。此功能旨在⽤于演⽰或调试⽬的。使⽤它来确保元素出现在页⾯上不是⼀个好主意。应改⽤上述超时和等待。有关⽀持的语法,请参见下⾯的时间格式。时间格式所有超时和等待时间都可以以秒为单位的数字(例如0.5或42)或使⽤Robot Framework的时间语法(例如1.5 seconds or 1 min 30s)来给出。有关时间语法的更多信息,请参见《 Robot Framework⽤户指南》。故障运⾏功能SeleniumLibrary具有⽅便的功能,如果其⾃⾝的任何关键字失败,它可以⾃动执⾏关键字。默认情况下,它使⽤Capture PageScreenshot关键字,但是在导⼊库时,可以通过使⽤Register Keyword在失败时运⾏关键字或与run_on_failure参数⼀起更改。可以使⽤任何导⼊的库或资源⽂件中的任何关键字。可以通过使⽤特殊值NOTHING或任何被认为是错误的值(请参阅布尔参数)(例如NONE)来禁⽤运⾏失败功能。布尔参数⼀些关键字接受作为布尔值true或false处理的参数。如果将这样的参数作为字符串给出,则如果该参数为空或不区分⼤⼩写,等于false,no,off,0或none,则将其视为false。不管其他字符串的值如何,都认为它们是正确的,并且使⽤与Python中相同的规则测试其他参数类型。Ture 的例⼦:Set Screenshot Directory KaTeX parse error: Expected 'EOF', got '#' at position 30: …ersist=True # 字符串通常为…{RESULTS} persist=yes # 与上述相同.Set Screenshot Directory
RESULTS persist={TRUE} # Python True为 Screenshot Directory
RESULTS persist={42} # ⾮0的数字为 的例⼦:Set Screenshot Directory KaTeX parse error: Expected 'EOF', got '#' at position 31: …rsist=False #字符串false为false.…{RESULTS} persist=no # 另外,字符串no为 Screenshot Directory KaTeX parse error: Expected 'EOF', got '#' at position 30: …ersist=NONE # 字符串NONE为false.…{RESULTS} persist=KaTeX parse error: Expected 'EOF', got '#' at position 12: {EMPTY} # 空字符串为…{RESULTS} persist=KaTeX parse error: Expected 'EOF', got '#' at position 12: {FALSE} # Python False为f…{RESULTS} persist=${NONE} # Python None为false.请注意,在SeleniumLibrary 3.0之前,所有⾮空字符串(包括false,no和none)都被视为true。从SeleniumLibrary 4.0开始,字符串0和off被视为false。WebDriver事件SeleniumLibrary提供对EventFiringWebDriver的⽀持。有关更多详细信息,请参阅Selenium和SeleniumLibraryEventFiringWebDriver⽀持⽂档。SeleniumLibrary 4.0中新增了EventFiringWebDriver。EventFiringWebDriver是Selenium提供的API的侦听器类型。 EventFiringWebDriver允许侦听Selenium API调⽤,并允许⽤户在Selenium API⽅法之前和之后触发事件。 请参阅Selenium EventFiringWebDriver⽂档,⽀持哪些Selenium API⽅法以及EventFiringWebDriver的⼯作⽅式。Selenium EventFiringWebDriver⽂档类:class _firing_iringWebDriver(driver, event_listener)[source]A wrapper around an arbitrary WebDriver instance which supports firing eventsCreates a new instance of the EventFiringWebDriverArgs:
driver : A WebDriver instanceevent_listener : Instance of a class that subclasses AbstractEventListener and implements it fully or partially例⼦:from ver import Firefoxfrom import EventFiringWebDriver, AbstractEventListenerclass MyListener(AbstractEventListener):def before_navigate_to(self, url, driver):print(“Before navigate to %s” % url)def after_navigate_to(self, url, driver):print(“After navigate to %s” % url)⽅法:back()[source]close()[source]execute_async_script(script, *args)[source]execute_script(script, *args)[source]find_element(by=‘id’, value=None)[source]find_element_by_class_name(name)[source]find_element_by_css_selector(css_selector)[source]find_element_by_id(id_)[source]find_element_by_link_text(link_text)[source]find_element_by_name(name)[source]find_element_by_partial_link_text(link_text)[source]find_element_by_tag_name(name)[source]find_element_by_xpath(xpath)[source]find_elements(by=‘id’, value=None)[source]find_elements_by_class_name(name)[source]find_elements_by_css_selector(css_selector)[source]find_elements_by_id(id_)[source]find_elements_by_link_text(text)[source]¶find_elements_by_name(name)[source]find_elements_by_partial_link_text(link_text)[source]find_elements_by_tag_name(name)[source]find_elements_by_xpath(xpath)[source]forward()[source]get(url)[source]quit()[source]wrapped_driverReturns the WebDriver instance wrapped by this EventsFiringWebDriver类:class _firing_iringWebElement(webelement, ef_driver)[source]” A wrapper around WebElement instance which supports firing eventsCreates a new instance of the EventFiringWebElement⽅法:clear()[source]click()[source]find_element(by=‘id’, value=None)[source]find_element_by_class_name(name)[source]find_element_by_css_selector(css_selector)[source]find_element_by_id(id_)[source]find_element_by_link_text(link_text)[source]find_element_by_name(name)[source]find_element_by_partial_link_text(link_text)[source]find_element_by_tag_name(name)[source]find_element_by_xpath(xpath)[source]find_elements(by=‘id’, value=None)[source]find_elements_by_class_name(name)[source]find_elements_by_css_selector(css_selector)[source]find_elements_by_id(id_)[source]find_elements_by_link_text(link_text)[source]find_elements_by_name(name)[source]find_elements_by_partial_link_text(link_text)[source]find_elements_by_tag_name(name)[source]find_elements_by_xpath(xpath)[source]send_keys(*value)[source]wrapped_elementReturns the WebElement wrapped by this EventFiringWebElement instance通过Selenium EventFiringWebDriver⽂档我们看可以看到,selenium的webdriver监听器可以在webdriver的实例前和实例后执⾏⼀些操作,封装的⽅法可以通过id、name、css、链接、xpath、tag来识别元素。线程⽀持SeleniumLibrary不是线程安全的。这主要是因为底层的Selenium⼯具在⼀个浏览器/驱动程序实例中不是线程安全的。由于Selenium⽅⾯的限制,SeleniumLibrary提供的关键字或API不是线程安全的。插件SeleniumLibrary提供插件,以作为修改和添加库关键字以及修改某些内部功能的⼀种⽅式,⽽⽆需创建新的库或修改源代码。有关更多详细信息,请参见插件API⽂档。插件API基于SeleniumLibrary 4.0的新版本引⼊我们在引⼊seleniumlibrary库的时候可以使⽤的参数列表:timeout=5.0implicit_wait=0.0run_on_failure=Capture Page Screenshotscreenshot_root_directory=Noneplugins=Noneevent_firing_webdriver=NoneSeleniumLibrary在引⼊时可以使⽤下⾯的参数设置:timeout: 与Wait …关键字⼀起使⽤的超时的默认值.implicit_wait: 定位元素时使⽤的隐式等待的默认值.run_on_failure: 定位元素时使⽤的隐式等待的默认值(Capture Page Screenshot是失败时进⾏截图).screenshot_root_directory: 创建可能的屏幕截图或EMBED的⽂件夹的路径。有关EMBED的更多详细信息,请参见设置屏幕快照⽬录关键字。如果未提供,则使⽤写⼊⽇志⽂件的⽬录.plugins: 允许使⽤外部Python类扩展_firing_webdriver: 使⽤EventFiringWebDriver包装selenium类.
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1688906805a182259.html
评论列表(0条)