Robot Framework使用Robot Framework/selenium获取后台调用 [英] Robot Frameworkget background call with Robot Framework/selenium

查看:68
本文介绍了Robot Framework使用Robot Framework/selenium获取后台调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 selenium 测试 Web 应用程序,我想检查是否有在后台完成的调用(post、get).例如我加载了 google.com,在开发者选项中我可以看到它做了一些请求.

我查看了 Robot Framework 中硒库的

I'm testing an web application with selenium, what I want to check is if there are calls done in the background (post,get). e.g. I load google.com and in the developer options I can see that it does some requests.

I looked into the documentation for the selenium library in Robot Framework but cant find an option. Is it possible to get the requests done? I also found selenium wire that does exactly what I want, but in all the examples they use a driver object, Is it possible to get the driver object used by Robot Framework/selenium?

e.g.

*** Settings ***
Documentation     Simple example using SeleniumLibrary.
Library           SeleniumLibrary

*** Variables ***
${LOGIN URL}      http://localhost:7272
${BROWSER}        Chrome

*** Test Cases ***
Valid Login
    Open Browser To Login Page
    Input Username    demo
    Input Password    mode
    Submit Credentials
    Welcome Page Should Be Open
    Get call to some-site should be done <---- what i want.
    [Teardown]    Close Browser

*** Keywords ***
Open Browser To Login Page
    Open Browser    ${LOGIN URL}    ${BROWSER}
    Title Should Be    Login Page

Input Username
    [Arguments]    ${username}
    Input Text    username_field    ${username}

Input Password
    [Arguments]    ${password}
    Input Text    password_field    ${password}

Submit Credentials
    Click Button    login_button

Welcome Page Should Be Open
    Title Should Be    Welcome Page

I tried the following based on Bence Kaulics's answwer

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

def get_logs2(driver):
# enable browser logging
    #d = DesiredCapabilities.CHROME
    #d['goog:loggingPrefs'] = { 'browser':'ALL' }
    #driver = webdriver.Chrome(desired_capabilities=d)

    # load the desired webpage
    #driver.get('http://34.90.50.21/')
    #driver.get(driver.current_url)
    a = driver.get_log('browser')

    # print messages
    for entry in driver.get_log('browser'):
        print(entry)
    print("finished")
    return a

the robot part I do is:

*** Keywords ***
   Get Logs2
        [Arguments]     ${arg1}
        ${seleniumlib}=    Get Library Instance    SeleniumLibrary
        Log    ${seleniumlib._drivers.active_drivers}[0]
        Get Logs2   ${seleniumlib._drivers.active_drivers}[0]

But to no avail

解决方案

In the SeleniumLibrary/init.py line 487, you can see that SeleniumLibrary has a member called _drivers which is an instance of class WebDriverCache. If you browse further you can see that this WebDriverCache has an active_drivers property. This is what you need.

To retrieve it you can use the Get Library Instance keyword that will return you the object of your SeleniumLibrary instance. The rest is accessible by the extended variable syntax.

You also have to set the appropriate browser capabilities to enable logging in the web driver.

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

def get_chrome_browser_logging_capability():
    d = DesiredCapabilities.CHROME
    d['goog:loggingPrefs'] = { 'browser':'ALL' }
    return d

def get_logs2(driver):
    return driver.get_log('browser')

Here is a quick example:

*** Settings ***
Library    SeleniumLibrary
Library    driver.py

*** Test Cases ***
Browser Log Cases
    ${capability}=    Get Chrome Browser Logging Capability
    Open Browser    https://stackoverflow.com    Chrome    desired_capabilities=${capability}
    Go To    https://stackoverflow.com/q/66155774/3820025
    ${seleniumlib}=    Get Library Instance    SeleniumLibrary
    ${message} =    Get Logs2   ${seleniumlib._drivers.active_drivers}[0]
    [Teardown]    Close All Browsers

With these I can see log entries returned.

这篇关于Robot Framework使用Robot Framework/selenium获取后台调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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