From 9e30d64816547347c090e46ab3219f3fbe818d76 Mon Sep 17 00:00:00 2001 From: Leonard Daume Date: Tue, 1 Aug 2017 21:48:32 +0200 Subject: [PATCH] The new config path webdriver.user.agent closes #4 --- README.md | 32 +++++-- build.sbt | 96 ++++++++++++------- project/plugins.sbt | 2 + .../headless/chrome/HeadlessDriver.java | 3 + .../chrome/HeadlessDriverUtilsTest.java | 8 +- src/test/resources/application.conf | 3 + src/test/resources/leona.conf | 1 - 7 files changed, 104 insertions(+), 41 deletions(-) create mode 100644 src/test/resources/application.conf delete mode 100644 src/test/resources/leona.conf diff --git a/README.md b/README.md index 98af93c..3484baf 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,17 @@ +| Build Status | Dependencies UpToDate | Latest Version | License | +|:------------:|:---------------------:|:--------------:|:-------:| +| [![Build Status](https://ci.reinvent-software.de/buildStatus/icon?job=Headless-Chrome-Build)](https://ci.reinvent-software.de/job/Headless-Chrome-Build) | [![Dependencies UpToDate](https://ci.reinvent-software.de/buildStatus/icon?job=Headless-Chrome-DependencyCheck)](https://ci.reinvent-software.de/job/Headless-Chrome-DependencyCheck) | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/software.reinvent/headless-chrome/badge.svg)](https://maven-badges.herokuapp.com/maven-central/software.reinvent/headless-chrome) | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) | + Headless Chrome ========== - [Abstract](#abstract) -- [Status](#status) - [Usage](#usage) + - [Repo](#repo) + - [SBT](#sbt) + - [Maven](#maven) - [Guice Binding](#guice-binding) - [Config](#config) - [Chrome](#chrome) @@ -23,13 +29,26 @@ Headless Chrome Google Chrome version 59+ contains a real headless mode with no need of any display like xvfb or vnc. -# Status +# Usage -| Build Status | Dependencies UpToDate | License | -|:------------:|:---------------------:|:-------:| -| [![Build Status](https://ci.reinvent-software.de/buildStatus/icon?job=Headless-Chrome-Build)](https://ci.reinvent-software.de/job/Headless-Chrome-Build) | [![Dependencies UpToDate](https://ci.reinvent-software.de/buildStatus/icon?job=Headless-Chrome-DependencyCheck)](https://ci.reinvent-software.de/job/Headless-Chrome-DependencyCheck) | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) | +## Repo +Just add the following [maven central](https://mvnrepository.com/artifact/software.reinvent/commons) dependency. + +### SBT +Add dependency to `build.sbt`. +```bash +libraryDependencies += "software.reinvent" % "headless-chrome" % "0.3.0" +``` + +### Maven +```xml + + software.reinvent + headless-chrome + 0.3.0 + false } - -publishTo := { - val nexus = "https://maven.reinvent-software.de/nexus/" - if ((version in ThisBuild).value.endsWith("SNAPSHOT")) { - Some("snapshots" at nexus + "content/repositories/snapshots") - else - Some("releases" at nexus + "content/repositories/releases") -} - -overridePublishBothSettings - -credentials += Credentials(Path.userHome / ".ivy2" / ".credentials") - -// Do not append Scala versions to the generated artifacts -crossPaths := false - -// This forbids including Scala related libraries into the dependency -autoScalaLibrary := false - -aetherOldVersionMethod := true - resolvers ++= Seq( - Resolver.mavenLocal, - "ReInvent Software OSS" at "https://maven.reinvent-software.de/nexus/content/groups/public" + Resolver.mavenLocal ) - libraryDependencies ++= Seq( // Commons @@ -65,4 +40,59 @@ scalacOptions in Test ++= Seq("-Yrangepos") dependencyUpdatesFailBuild := true -//dependencyUpdatesExclusions := moduleFilter(organization = "software.reinvent") +// Enables publishing to maven repo +publishMavenStyle := true +publishArtifact in Test := false +pomIncludeRepository := { _ => false } + +enablePlugins(SignedAetherPlugin) + +disablePlugins(AetherPlugin) + +publishTo := { + val nexus = "https://maven.reinvent-software.de/nexus/" + if (version.value.trim.endsWith("SNAPSHOT")) { + Some("snapshots" at nexus + "content/repositories/snapshots") + } else { + Some(Opts.resolver.sonatypeStaging) + } +} + +overridePublishBothSettings + +overridePublishSignedBothSettings + +credentials += Credentials(Path.userHome / ".ivy2" / ".credentials") +//credentials += Credentials(Path.userHome / ".ivy2" / ".credentials.sonatype") + +// Do not append Scala versions to the generated artifacts +crossPaths := false + +// This forbids including Scala related libraries into the dependency +autoScalaLibrary := false + +homepage := Some(new URL("https://github.com/ldaume/headless-chrome")) + +startYear := Some(2017) + +licenses := Seq(("MIT", new URL("https://github.com/ldaume/headless-chrome/blob/master/LICENSE"))) + +pomExtra <<= (pomExtra, name, description) { (pom, name, desc) => + pom ++ xml.Group( + + http://github.com/ldaume/headless-chrome/tree/master + scm:git:git://github.com:ldaume/headless-chrome.git + scm:git:git@github.com:ldaume/headless-chrome.git + + + + ldaume + Leonard Daume + https://reinvent.software + + + ) +} + + + diff --git a/project/plugins.sbt b/project/plugins.sbt index cc88a15..421b228 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,3 +1,5 @@ addSbtPlugin("no.arktekk.sbt" % "aether-deploy" % "0.19.0") addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.3.1") + +addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0") \ No newline at end of file diff --git a/src/main/java/software/reinvent/headless/chrome/HeadlessDriver.java b/src/main/java/software/reinvent/headless/chrome/HeadlessDriver.java index 45c1d2f..f02431d 100644 --- a/src/main/java/software/reinvent/headless/chrome/HeadlessDriver.java +++ b/src/main/java/software/reinvent/headless/chrome/HeadlessDriver.java @@ -66,6 +66,9 @@ public HeadlessDriver(Config config) { } else { windowSize = "1920,1200"; } + if (configToUse.hasPath("webdriver.user.agent")) { + chromeOptions.addArguments("--user-agent=" + configToUse.getString("webdriver.user.agent")); + } chromeOptions.addArguments("--headless", "--disable-gpu", "--no-sandbox", "--incognito", "window-size=" + windowSize); final DesiredCapabilities capabilities = DesiredCapabilities.chrome(); diff --git a/src/test/java/software/reinvent/headless/chrome/HeadlessDriverUtilsTest.java b/src/test/java/software/reinvent/headless/chrome/HeadlessDriverUtilsTest.java index 0dee56e..02978be 100644 --- a/src/test/java/software/reinvent/headless/chrome/HeadlessDriverUtilsTest.java +++ b/src/test/java/software/reinvent/headless/chrome/HeadlessDriverUtilsTest.java @@ -50,7 +50,7 @@ public void testChromeDriver() throws Exception { public void testExpectedElement() throws Exception { driver.get("https://reinvent-software.de"); final String text = driver.findElementByXPath(".//*[@id='team']//p[@class='text-muted']").getText(); - assertThat(text).isEqualTo("Head of Development"); + assertThat(text).isEqualTo("Software Craftsman | DevOp | Agile Evangelist | Responsible Leader"); } @Test @@ -63,5 +63,11 @@ public void testScreenshot() throws Exception { } + @Test + public void testUserAgent() throws Exception { + driver.get("http://www.useragents.com/"); + final String text = driver.findElementByXPath("./html/body/center/p").getText(); + assertThat(text).contains("Mozilla/5.0 (X11; Linux i686; rv:17.0.1) Gecko/20100101 Arch Linux Firefox/17.0.1"); + } } diff --git a/src/test/resources/application.conf b/src/test/resources/application.conf new file mode 100644 index 0000000..f73989a --- /dev/null +++ b/src/test/resources/application.conf @@ -0,0 +1,3 @@ +webdriver.chrome.binary: "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" + +webdriver.user.agent: "Mozilla/5.0 (X11; Linux i686; rv:17.0.1) Gecko/20100101 Arch Linux Firefox/17.0.1" \ No newline at end of file diff --git a/src/test/resources/leona.conf b/src/test/resources/leona.conf deleted file mode 100644 index 86c79ed..0000000 --- a/src/test/resources/leona.conf +++ /dev/null @@ -1 +0,0 @@ -webdriver.chrome.binary: "C:/Users/leona/Documents/dev/GoogleChromePortableBeta_60/GoogleChromePortable.exe" \ No newline at end of file