MikeHT (137) [Avatar] Offline
#1
If you are using a recent version of Firefox, there are issues with running Selenium. There are solutions posted, but I couldn't get them to work. So I switched to using Chrome. You can switch to using other browsers by following a similar process to that below.
Note I'm running on a Mac, so if you are on a PC, some of the steps might be a bit different.

First, with later versions of Selenium, you must also have a driver for your browser, I download the chromedriver_mac64.zip at https://sites.google.com/a/chromium.org/chromedriver/. Put this somewhere on your hard drive. When you unzip this, you get a file named chromedriver (chromedriver.exe on Windows).

In the pom file, change

		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-firefox-driver</artifactId>
			<version>${seleniumVersion}</version>
			<scope>test</scope>
		</dependency>


to

		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-java</artifactId>
			<version>${seleniumVersion}</version>
			<scope>test</scope>
		</dependency>


This will give you all the dependencies required to run not only FireFox, but Chrome, Opera, Safari and other browsers. In other words, <artifactId>selenium-java</artifactId> includes <artifactId>selenium-firefox-driver</artifactId> and the corresponding dependencies for other browsers. I also updated the versions of Selenium and Geb to the latest versions at the end of the pom file:

    <gebVersion>1.1.1</gebVersion>
    <seleniumVersion>3.5.2</seleniumVersion>


Then notice the project has a file src/test/resources/GebConfig.groovy. The book does not discuss this file, but it controls the browser that the test will run. I modified this file as shown below.

import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.chrome.ChromeDriver

//driver = { new FirefoxDriver() }
driver = { new ChromeDriver() }

baseUrl = "http://localhost:9081"
reportsDir = "target/geb-reports"


Then open the run configuration for the test. Right click on the test file name (AddProductSpec.groovy) and select "Run As>Run Configurations" and on the Arguments tab, enter the following in the VM arguments box

-Dwebdriver.chrome.driver=/Users/mhtabak/ChromeDriver/2.3.1/chromedriver

This is the location of the chromedriver downloaded earlier.

Then run the project on a server and you should be able to run the test. I was then able to successfully run the test and launch Chrome. I got the following results.

Test 1

Condition not satisfied:

$(".col1").$("h2").text() == "New Product details"
| | | |
[] [] null false

at com.manning.spock.AddProductGebSpec.Navigation to page(AddProductGebSpec.groovy:24)


Test 2

Condition not satisfied:

$(".ok").text() == "You added new product named: Bravia TV."
| | |
[] null false

at com.manning.spock.AddProductGebSpec.Creation of new product(AddProductGebSpec.groovy:34)

Not yet sure why the tests are failing.


MikeHT (137) [Avatar] Offline
#2
I figured out why the tests were failing. For some reason, in the second test, the Browser is launched with:

		Browser.drive {
			go "/web-ui-example/add-product.html"
		}


This should be

		Browser.drive {
			go "http://localhost:8080/web-ui-example/add-product.html"
		}


I'm not sure if I inadvertently removed the "http://localhost:8080" or it was in the source as downloaded, but the tests now pass.

Would love to know how to get this to run with Firefox, there seems to be a proxy issue I can't solve.
MikeHT (137) [Avatar] Offline
#3
After looking more carefully into things, it was not necessary to make the above change to the URL in the test files. This is what the baseUrl variable in the GebConfig.groovy file is for. There are also other solutions for getting the chromedriver. You can add the following code to the test files:

	def setupSpec() {
		System.setProperty("webdriver.chrome.driver",
			"/Users/mht/Chromedriver/2.3.1/chromedriver")
	}


Where "/Users/mht/Chromedriver/2.3.1/chromedriver" is the location where you downloaded the driver.

This solves the problem of where the driver is when you either run a test from a test file directly, or run with "mvn verify" from the command line.

If you don't add the above code to the the test files, to run from the command line, include the following on the command line:

-DargLine="-Dwebdriver.chrome.driver=/Users/mhtabak/ChromeDriver/2.3.1/chromedriver"

If you want to run with a build configuration that has a goal of "verify", click on the project and select “Run As>Run Configurations>Maven Build" and create a new configuration. Give it a goal of "verify" and enter the above -DargLine into the VM argument list.

Be sure to change the baseUrl in GebConfig.groovy to that which has the port number matching the port number in the pom file. You can't use the -Dwebdriver.chrome.driver argument when running this way as Maven arguments are not passed to the surefire and failsafe configurations, and both fork new JMVs which don’t get the arguments.
Kostis Kapelonis (63) [Avatar] Offline
#4
Hello

Many thanks for these updated instructions! I am sure that a lot of readers will find them useful.

That is the problem with all technology books. Some instructions no longer apply or become obsolete when the book actually reaches your hands.

Kostis