机器人框架,使用命令行标志打开浏览器以保存浏览器日志



我正在尝试将浏览器(chrome)日志消息(React 日志,js错误等)保存到机器人框架测试运行中的文件中。显然,这比应有的困难得多,因为我无法在机器人框架中找到方便的API来实现这一点。

我想出chrome可以用--enable-logging --v=1标志启动,以将日志消息定向到文件中。但是我如何通过机器人框架中的Open Browser关键字传递此标志呢?更准确地说,我实际上是使用robotframework-maven-plugin在远程服务器上使用无头浏览器运行测试。

这就是当前通过执行远程 shell 任务从 jenkins 在远程服务器上启动测试的方式

rm -rf tests
git clone git@*****/tests.git && cd tests
Xvfb :99 -ac -screen 0 1280x1024x24 &
export DISPLAY=:99
mvn -DforceOpenJpaExecution=true -Dbrowser=chrome -Dserver=***** -DchromeDriverPath=/usr/local/bin/chromedriver clean verify

这是测试项目的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>*****</groupId>
<artifactId>tests</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>tests</name>
<dependencies>
<dependency>
<groupId>com.github.markusbernhardt</groupId>
<artifactId>robotframework-selenium2library-java</artifactId>
<version>1.4.0.8</version>
</dependency>
</dependencies>
<build>
<defaultGoal>verify</defaultGoal>        
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<id>setPropertyChromeDriver</id>
<phase>integration-test</phase>
<goals>
<goal>set-system-properties</goal>
</goals>
<configuration>
<properties>
<property>
<name>webdriver.chrome.driver</name>
<value>${chromeDriverPath}</value>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.robotframework</groupId>
<artifactId>robotframework-maven-plugin</artifactId>
<version>${robotframework-maven-plugin.version}</version>
<executions>
<execution>
<id>robotTest</id>  
<phase>integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> 

这是resource.robot的摘录

Open the web application
Open Browser    %{server}/App    %{browser}
Maximize Browser Window
Wait Until Page Contains        Login

现在,如何将--enable-logging --v=1传递给chrome驱动程序,或者是否有更方便的方法来将哑浏览器日志保存到文件或测试结果?

我找到了这段代码来设置标志,但我不知道如何在我的情况下在基于关键字的资源中应用它

from selenium import webdriver
options = webdriver.ChromeOptions()
# set some options
# for example:
# options.add_argument('--disable-logging')
driver = webdriver.Remote(desired_capabilities=options.to_capabilities())

当您需要使用Selenium2Library将选项传递给Webdriver时,您应该使用关键字Create Webdriver(以及后来的Go Tourl)。

在之前的SO问题中,我提供了一个也应该适用于这里的答案

*** Settings ***
Library    Selenium2Library
*** Test Cases ***
Log Chrome Console
${c_opts} =  Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
Call Method     ${c_opts}   add_argument             enable-logging 
Call Method     ${c_opts}   add_argument             v=1
Create Webdriver    Chrome    crm_alias    chrome_options=${c_opts}
Go To    http://www.url.com
[Teardown]  Close All Browsers

这会将参数添加到 Chrome 的命令行中。

最新更新