Selenide is nice wrapper around selenium web driver allowing to simplify writing UI tests with Selenium.
Some of the cook features are:
- jquery-like selector syntax, e.g.
$("div.myclass").is(Condition.visible) - Automatic screenshots on assertion failure
- Easy starting Selenium WebDriver
- And others
So, let’s write some tests on selenide and make it run from maven in a normal browser or in headless mode.
First, let’s create pom.xml:
xml
1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>com.github.kpavlov</groupId>
8 <artifactId>selenide-sample</artifactId>
9 <version>1.0-SNAPSHOT</version>
10
11 <properties>
12 <java.version>1.8</java.version>
13 <maven.compiler.source>${java.version}</maven.compiler.source>
14 <maven.compiler.target>${java.version}</maven.compiler.target>
15 <selenium.hub.url>http://local.example.com:4444/wd/hub</selenium.hub.url>
16 <holdBrowserOpen>false</holdBrowserOpen>
17 <surefire.argLine>-Dbrowser=${browser} -Dselenide.holdBrowserOpen=${holdBrowserOpen}</surefire.argLine>
18 </properties>
19
20 <prerequisites>
21 <maven>3.3</maven>
22 </prerequisites>
23
24 <profiles>
25 <profile>
26 <id>firefox</id>
27 <activation>
28 <activeByDefault>true</activeByDefault>
29 </activation>
30 <properties>
31 <browser>firefox</browser>
32 </properties>
33 </profile>
34 <profile>
35 <id>chrome</id>
36 <properties>
37 <browser>chrome</browser>
38 </properties>
39 </profile>
40 <profile>
41 <id>phantomjs</id>
42 <properties>
43 <browser>phantomjs</browser>
44 </properties>
45 </profile>
46 <profile>
47 <id>ie</id>
48 <properties>
49 <browser>ie</browser>
50 </properties>
51 </profile>
52 <profile>
53 <id>htmlunit</id>
54 <properties>
55 <browser>htmlunit</browser>
56 </properties>
57 <dependencies>
58 <dependency>
59 <groupId>org.seleniumhq.selenium</groupId>
60 <artifactId>selenium-htmlunit-driver</artifactId>
61 <version>LATEST</version>
62 <scope>test</scope>
63 </dependency>
64 </dependencies>
65 </profile>
66 <profile>
67 <id>ci-server</id>
68 <properties>
69 <surefire.argLine>-Dremote=${selenium.hub.url} -Dbrowser=${browser}</surefire.argLine>
70 </properties>
71 </profile>
72 <profile>
73 <id>local</id>
74 <properties>
75 <holdBrowserOpen>true</holdBrowserOpen>
76 </properties>
77 </profile>
78 </profiles>
79
80 <build>
81 <defaultGoal>clean test</defaultGoal>
82 <plugins>
83 <plugin>
84 <groupId>org.apache.maven.plugins</groupId>
85 <artifactId>maven-surefire-plugin</artifactId>
86 <version>2.19.1</version>
87 <configuration>
88 <argLine>${surefire.argLine}</argLine>
89 </configuration>
90 </plugin>
91 </plugins>
92 <pluginManagement>
93 <plugins>
94 <plugin>
95 <groupId>org.apache.maven.plugins</groupId>
96 <artifactId>maven-surefire-plugin</artifactId>
97 </plugin>
98 </plugins>
99 </pluginManagement>
100 </build>
101
102 <dependencies>
103 <dependency>
104 <groupId>org.slf4j</groupId>
105 <artifactId>slf4j-simple</artifactId>
106 <version>1.7.13</version>
107 <scope>test</scope>
108 </dependency>
109 <dependency>
110 <groupId>com.codeborne</groupId>
111 <artifactId>selenide</artifactId>
112 <version>3.5.1</version>
113 <scope>test</scope>
114 </dependency>
115 <dependency>
116 <groupId>junit</groupId>
117 <artifactId>junit</artifactId>
118 <version>4.12</version>
119 <scope>test</scope>
120 </dependency>
121 </dependencies>
122</project>Notice the properties:
- surefire.argLine – defines a command line parameters for running tests.
- browser – specifies a browser to use.
- selenium.hub.url – URL where selenium hub is running, for tests with remote web driver, e.g. on CI server.
- holdBrowserOpen – should the browser be closed after tests.
Also there are a fistful of profiles to use as a shortcuts, e.g.
mvn clean test -Pphantomjs– run tests locally with phantomjs headless browser. Requires phantomjs to be installedmvn clean test -Pfirefox,local– run tests locally with Firefox and leaves a browser open after testsmvn clean test -Pci-server,chrome– run tests on selenium hub with Chrome
You may find working example in my GitHub repository.