【问题标题】:Run Selenium tests against an Angular application in headless Chrome在无头 Chrome 中针对 Angular 应用程序运行 Selenium 测试
【发布时间】:2018-08-29 19:40:19
【问题描述】:

我正在尝试针对 Angular 应用程序创建 Selenium 测试。我希望这些测试在我的持续集成构建中运行,这需要在无头模式下运行它们。我正在尝试使用无头 Chrome。

我创建了两个简单的 Selenium 测试。一个测试访问我的本地 Angular 应用程序,另一个访问 Google。当我使用 Chrome GUI 运行它们时,它们都运行良好,但是当我切换到无头 Chrome 时,Angular 测试失败。

我在 Angular 测试中打印出页面源,并且在使用 Chrome GUI 时所有 HTML 都正确呈现,但是当我切换到无头时,页面源仅显示空的 headbody 标记。

为什么会发生这种情况,我该如何解决?

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ExampleSeleniumTest {

    @LocalServerPort
    private int port;

    @Autowired
    private WebDriver driver;

    @Test // Always works
    public void testGoogle() {
        driver.get("http://www.google.com");
        WebElement element = driver.findElement(By.name("q"));
        element.sendKeys("Cheese!");
        element.submit();

        List<WebElement> findElements = driver.findElements(By.xpath("//*[@id='rso']//h3/a"));

        for (WebElement webElement : findElements) {
            System.out.println(webElement.getAttribute("href"));
        }
        Assert.assertTrue(findElements.size > 0);
    }


    @Test // Breaks in headless mode
    public void testLocalAngular() {
        driver.get("localhost:" + port);
        String pageSource = driver.getPageSource();
        // Page source empty in headless mode
        System.out.println(pageSource);
        String title = driver.findElement(By.className("mat-card-title")).getText();
        Assert.assertEquals("My Starter", title);
    }
} 

这些测试依赖于以下配置

@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
@ComponentScan(basePackageClasses = MyStarterApplication.class)
public class SeleniumConfig {

    @Value("${selenium.headless:#{true}}")
    private boolean headless;   

    @EventListener(classes = ApplicationReadyEvent.class)
    public void prepareDriver() {
        // Using https://github.com/bonigarcia/webdrivermanager
        ChromeDriverManager.getInstance().setup();
    }

    @Bean(destroyMethod = "quit")
    public WebDriver webDriver() {
        ChromeOptions options = new ChromeOptions();
        if (headless) {
            options.addArguments("headless");
            options.addArguments("window-size=1200x600");
        }
        WebDriver driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        return driver;
    }

}

【问题讨论】:

    标签: angular selenium spring-boot integration-testing google-chrome-headless


    【解决方案1】:

    问题是由以下原因引起的:

    driver.get("localhost:" + port);
    

    我需要指定协议。如果我使用,我的测试用例可以工作:

    driver.get("http://localhost:" + port);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      相关资源
      最近更新 更多