Skip to content
Andrei Solntsev edited this page Mar 12, 2023 · 5 revisions

You've developed some tests with IDE and want to run them from command line / CI server.

In this case you need a build script. De-facto standards in Java world are Gradle and Maven. Some hippies prefer Bazel. And at good old days people used to use Ant.

Just in case, Selenide itself uses Gradle as the youngest one. :)

Gradle

A build script example from Hangman game:

dependencies {
  testImplementation 'junit:junit:4.13.2'
  testImplementation 'com.codeborne:selenide:6.12.2'
  testImplementation group: 'com.codeborne', name: 'phantomjsdriver', version: '1.2.1', transitive: false
}

test { // for running UNIT-tests
  include 'ee/era/hangman/**'
}

task uitest(type: Test) {
  systemProperties['browser'] = 'firefox'
}

task ie(type: Test) {
  systemProperties['browser'] = 'ie'
  systemProperties['timeout'] = '12000'
}

task htmlunit(type: Test) {
  systemProperties['browser'] = 'htmlunit'
}

task chrome(type: Test) {
  systemProperties['browser'] = 'chrome'
  systemProperties['webdriver.chrome.driver'] = '/usr/bin/chromedriver'
}

task phantomjs(type: Test) {
  systemProperties['browser'] = 'phantomjs'
}

tasks.withType(Test).all { testTask ->
  testTask.systemProperties['file.encoding'] = 'UTF-8'
  testTask.testLogging.showStandardStreams = true
  testTask.outputs.upToDateWhen { false }
}

Maven

A build script example from Google search sample project:

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.codeborne</groupId>
      <artifactId>selenide-junit4</artifactId>
      <version>6.12.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>2.0.6</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <profiles>
    <profile>
      <id>chrome</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <systemPropertyVariables>
                <selenide.browser>chrome</selenide.browser>
                <selenide.headless>true</selenide.headless>
              </systemPropertyVariables>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>

    <profile>
      <id>firefox</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <systemPropertyVariables>
                <selenide.browser>firefox</selenide.browser>
                <selenide.headless>true</selenide.headless>
              </systemPropertyVariables>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

P.S. Yes, I know: Maven syntax is toooo verbooooose....

ANT

Here is an example for running UNIT-tests and UI-tests:

  <target name="test" depends="compile-tests">
    <mkdir dir="test-results"/>
    <junit maxmemory="512m" haltonfailure="false" failureproperty="tests-failed" fork="true">
      <batchtest todir="test-results">
        <fileset dir="classes-test" includes="**/*Test.class" excludes="**/Abstract*"/>
        <formatter type="xml"/>
        <formatter type="plain" usefile="false"/>
      </batchtest>
      <classpath>
        <path path="classes"/>
        <path path="classes-test"/>
        <fileset dir="lib" includes="**/*.jar"/>
      </classpath>
    </junit>
    <fail if="tests-failed"/>
  </target>

  <target name="ui-test" depends="compile-ui-tests">
    <mkdir dir="ui-test-results"/>
    <junit maxmemory="512m" haltonfailure="false" failureproperty="tests-failed" fork="true">
      <batchtest todir="ui-test-results">
        <fileset dir="classes-ui-test" includes="**/*Test.class" excludes="**/Abstract*"/>
        <formatter type="xml"/>
        <formatter type="plain" usefile="false"/>
      </batchtest>
      <classpath>
        <path path="classes"/>
        <path path="classes-ui-test"/>
        <fileset dir="lib" includes="**/*.jar"/>
      </classpath>
    </junit>
    <fail if="tests-failed"/>
  </target>