GVKun编程网logo

Spring Boot GUI测试Selenium WebDriver(spring boot test)

10

在本文中,我们将带你了解SpringBootGUI测试SeleniumWebDriver在这篇文章中,我们将为您详细介绍SpringBootGUI测试SeleniumWebDriver的方方面面,并解

在本文中,我们将带你了解Spring Boot GUI测试Selenium WebDriver在这篇文章中,我们将为您详细介绍Spring Boot GUI测试Selenium WebDriver的方方面面,并解答spring boot test常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的angularjs – 使用Maven,Protractor和Selenium WebDriver进行集成测试、JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测、NODE.JS selenium-webdriver 远程测试、selenium webdriver

本文目录一览:

Spring Boot GUI测试Selenium WebDriver(spring boot test)

Spring Boot GUI测试Selenium WebDriver(spring boot test)

我开发了一个Spring Boot / Angular JS应用程序。现在,我正在尝试实现一些GUI界面测试。

我尝试使用Selenium ChromeDriver,因此添加了Selenium依赖项:

<dependency>    <groupId>org.seleniumhq.selenium</groupId>    <artifactId>selenium-java</artifactId>    <version>3.4.0</version></dependency>

我创建了第一个测试:

@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = MyMainClass.class)public class SeleniumTest {    private WebDriver driver;    @Before    public void setup() {        System.setProperty("webdriver.chrome.driver", "my/path/to/chomedriver");        driver = new ChromeDriver();    }    @Test    public void testTest() throws Exception {        driver.get("https://www.google.com/");    }}

这很好。但是现在我想让我的应用页面具有:

driver.get("http://localhost:8080/");

但是我在Chrome浏览器中看到了“ ERR_CONNECTION_REFUSED”。

我认为这是因为我需要先设置测试才能运行Web应用程序,然后才能运行测试,但是我找不到实现该目标的方法?

答案1

小编典典

在您的情况下,服务未启动。尝试这样的事情。

@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)public class SeleniumTest {    @LocalServerPort    private int port;    private WebDriver driver;    @Value("${server.contextPath}")    private String contextPath;    private String base;    @Before    public void setUp() throws Exception {        System.setProperty("webdriver.chrome.driver", "my/path/to/chromedriver");        driver = new ChromeDriver();        this.base = "http://localhost:" + port;    }    @Test    public void testTest() throws Exception {        driver.get(base + contextPath);    }}

更新:

添加依赖项

    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <scope>test</scope>    </dependency>

angularjs – 使用Maven,Protractor和Selenium WebDriver进行集成测试

angularjs – 使用Maven,Protractor和Selenium WebDriver进行集成测试

我们开发了一个Web应用程序,它在后端使用 Java,在Angular中使用UI,使用Maven作为构建系统.

我一直在尝试使用Protractor设置自动集成测试,并且在加载Googling / StackOverflowing之后仍然无法弄清楚如何实现端到端配置.

Node.js / NPM安装(失败)

我已经尝试使用frontend-maven-plugin来处理Node.js和NPM安装,但由于我们是在公司防火墙后面,所以似乎无法直接下载任何东西.它可以从我们的Artifactory下载Node但是在NPM下载时失败了(我不明白为什么它甚至下载它因为它是Node包的一部分).无论如何,我放弃了这个想法,并决定使用本地安装的Node.

启动Tomcat

启动/停止用于e2e测试的Tomcat实例可以很好地处理

<plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <url>${tomcat.manager.url}</url>
                <path>/</path>
                <server>Tomcat</server>
            </configuration>
            <executions>
                <!-- Starting Tomcat -->
                <execution>
                    <id>start-tomcat</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <!-- Fork the process,otherwise the build will be blocked by the running Tomcat -->
                        <fork>true</fork>
                        <port>${tomcat.port}</port>
                        <systemProperties>
                            <!-- We want to use the 'e2e' profile for integration testing -->
                            <spring.profiles.active>e2e</spring.profiles.active>
                        </systemProperties>
                    </configuration>
                </execution>
                <!-- Stopping Tomcat -->
                <execution>
                    <id>stop-tomcat</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>shutdown</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

使用WebDriver(失败)

我设法启动WebDriver,但问题是它阻止了任何进一步的执行:

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
                <!-- Start webdriver -->
                <execution>
                    <id>start-webdriver</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>webdriver-manager</executable>
                        <arguments>
                            <argument>start</argument>
                        </arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>

运行量角器

鉴于安装了Node.js并且WebDriver正在运行,这应该不是问题.但由于我无法启动WebDriver以便继续执行,因此会被阻止.

有关WebDriver如何管理(启动/停止)的任何建议?

解决方法

将directConnect:true添加到Protractor配置文件中可以解决启动/停止WebDriver的问题(如Nick所建议的).在这种情况下,必须从POM中删除对WebDriver的任何显式控制.

可用参数在reference configuration file中有详细说明.

JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测

JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测

如何解决JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测?

从当前的实现开始,一种理想的访问网页而不被检测到的方法是使用ChromeOptions()该类向以下参数添加几个参数:

排除enable-automation开关的集合 关掉 useAutomationExtension 通过以下实例ChromeOptions

Java示例:

System.setProperty("webdriver.chrome.driver", "C:\\Utility\\browserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
WebDriver driver =  new ChromeDriver(options);
driver.get("https://www.google.com/");

Python范例

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option(''useAutomationExtension'', False)
driver = webdriver.Chrome(options=options, executable_path=r''C:\path\to\chromedriver.exe'')
driver.get("https://www.google.com/")

解决方法

我正在尝试使用selenium和铬在网站中自动化一个非常基本的任务,但是以某种方式网站会检测到铬是由selenium驱动的,并阻止每个请求。我怀疑该网站是否依赖像这样的公开DOM变量https://stackoverflow.com/a/41904453/648236来检测selenium驱动的浏览器。

我的问题是,有没有办法使navigator.webdriver标志为假?我愿意尝试修改后重新尝试编译selenium源,但是似乎无法在存储库中的任何地方找到NavigatorAutomationInformation源https://github.com/SeleniumHQ/selenium

任何帮助深表感谢

PS:我还从https://w3c.github.io/webdriver/#interface尝试了以下操作

Object.defineProperty(navigator,''webdriver'',{
    get: () => false,});

但是它仅在初始页面加载后更新属性。我认为网站会在执行脚本之前检测到变量。

NODE.JS selenium-webdriver 远程测试

NODE.JS selenium-webdriver 远程测试

《NODE.JS selenium-webdriver 远程测试》要点:
本文介绍了NODE.JS selenium-webdriver 远程测试,希望对您有用。如果有疑问,可以联系我们。

NODE.JS selenium-webdriver 远程测试从一台测试机遥控另台selenium-server-standalone

var webdriver = require('/usr/local/lib/node_modules/selenium-webdriver');

var builder = new webdriver.Builder().  
  usingServer('http://192.168.6.20:4444/wd/hub').  
  withCapabilities(webdriver.Capabilities.firefox());  

var driver = builder.build();  

driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnK')).click();
driver.wait(function() {
  return driver.getTitle().then(function(title) {
    return title === 'webdriver - Google Search';
  });
}, 1000);

driver.quit();

Unit Test

安装 mocha

npm install -g mocha

测试脚本

var assert = require('assert'),test = require('selenium-webdriver/testing'),webdriver = require('selenium-webdriver');

test.describe('Google Search', function() {

 test.it('should work', function() {
  //var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
  var driver = new webdriver.Builder().usingServer('http://192.168.6.20:4444/wd/hub').withCapabilities(webdriver.Capabilities.firefox()).build();  
  driver.get('http://www.google.com');
  var searchBox = driver.findElement(webdriver.By.name('q'));
  searchBox.sendKeys('simple programmer');
  searchBox.getAttribute('value').then(function(value) {
   assert.equal(value, 'simple programmer');
  });
  driver.quit();
 });
});

运行测试程序

# mocha mocha.js 
  1 passing (10s)

测试成功返回1 passing (10s)

selenium webdriver

selenium webdriver

 

chrome webdriver 下载
http://chromedriver.storage.googleapis.com/index.html?path=2.46/
or
http://npm.taobao.org/mirrors/chromedriver/
下载后把文件解压,然后放到本机chrome浏览器文件路径里


firefox webdriver
https://github.com/mozilla/geckodriver/releases


phantomjs
https://phantomjs.org/download


#安装 selenium
pip3.7 install selenium

修改chrome webdriver路径可以在 selenium/webdriver/chrome/webdriver.py 文件中,修改 executable_path 

今天关于Spring Boot GUI测试Selenium WebDriverspring boot test的讲解已经结束,谢谢您的阅读,如果想了解更多关于angularjs – 使用Maven,Protractor和Selenium WebDriver进行集成测试、JavaSelenium Webdriver:修改navigator.webdriver标志以防止selenium检测、NODE.JS selenium-webdriver 远程测试、selenium webdriver的相关知识,请在本站搜索。

本文标签: