Ottenere l'esempio Selenium-WebDriver per funzionare


Sono completamente nuovo al Selenio e voglio solo installare ed eseguire l'esempio per cominciare, come descritto qui: http://www.seleniumhq.org/docs/03_webdriver.jsp Ho installato Java SE 9.0.1 JDK, Eclipse IDE per sviluppatori Java e Apache Maven. Quindi ho creato un pom.file xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>MySel20Proj</groupId>
        <artifactId>MySel20Proj</artifactId>
        <version>1.0</version>
        <dependencies>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-server</artifactId>
                <version>3.0.1</version>
            </dependency>
        </dependencies>
</project>

Poi ho fatto mvn clean install

Poi ho importato il progetto Maven in Eclipse.

Ho anche installato il plugin m2eclipse.

Ho chiamato il progetto MySel20Proj.

Qui, ho creato una classe: Selenium2Example.java

Qui, ho incollato il codice dal manuale:

package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium2Example {
     public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new FirefoxDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());

        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());

        //Close the browser
        driver.quit();
    }

}

Le istruzioni sembrano fermarsi qui. Cosa faccio dopo? Come faccio a gestirlo?

Aggiornamento 11/12/2017

Ora sto facendo qualche progresso con questo. Il progetto è finalmente in esecuzione, anche se non sta ancora eseguendo il test.

Questo è il mio ultimo file POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
      </properties>         
    <modelVersion>4.0.0</modelVersion>
    <groupId>MySel20Proj</groupId>
    <artifactId>MySel20Proj</artifactId>
    <version>1.0</version>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.8.1</version>
        </dependency>
    </dependencies>
     <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20.1</version>
            <configuration>
              <includes>
                <include>Selenium2Example.java</include>
              </includes>
            </configuration>
          </plugin>
        </plugins>
      </build>
</project>

Questo è il codice java:

//package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium2Example {
 public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        System.out.println("Hello world!");
        System.setProperty("webdriver.gecko.driver", 
"C:\\Users\\sstaple\\Downloads\\geckodriver\\geckodriver.exe");

        WebDriver driver = new FirefoxDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the 
element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());

        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>
() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());

        //Close the browser
        driver.quit();
    }

}

Questo era il risultato dell'esecuzione di mvn install nel prompt dei comandi:

> C:\Users\sstaple\eclipse-workspace\MySel20Proj>mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MySel20Proj 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MySel20Proj ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MySel20Proj ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MySel20Proj ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MySel20Proj ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Users\sstaple\eclipse-workspace\MySel20Proj\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.20.1:test (default-test) @ MySel20Proj ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running Selenium2Example
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 s - in Selenium2Example
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MySel20Proj ---
[INFO] Building jar: C:\Users\sstaple\eclipse-workspace\MySel20Proj\target\MySel20Proj-1.0.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ MySel20Proj ---
[INFO] Installing C:\Users\sstaple\eclipse-workspace\MySel20Proj\target\MySel20Proj-1.0.jar to C:\Users\sstaple\.m2\repository\MySel20Proj\MySel20Proj\1.0\MySel20Proj-1.0.jar
[INFO] Installing C:\Users\sstaple\eclipse-workspace\MySel20Proj\pom.xml to C:\Users\sstaple\.m2\repository\MySel20Proj\MySel20Proj\1.0\MySel20Proj-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.089 s
[INFO] Finished at: 2017-12-11T11:31:42Z
[INFO] Final Memory: 16M/55M
[INFO] ------------------------------------------------------------------------

Aggiorna 2 - 11/12/2017

Ha dovuto aggiungere un sacco di dipendenze al file pom per farlo compilare in modo pulito:

<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>3.8.1</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib-nodep</artifactId>
        <version>3.2.4</version>
    </dependency>
    <dependency>
        <groupId>com.codeborne</groupId>
        <artifactId>phantomjsdriver</artifactId>
        <version>1.3.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.3.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>21.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.netty/netty -->
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty</artifactId>
        <version>3.5.7.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform -->
    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna-platform</artifactId>
        <version>4.1.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/net.sourceforge.cssparser/cssparser -->
    <dependency>
        <groupId>net.sourceforge.cssparser</groupId>
        <artifactId>cssparser</artifactId>
        <version>0.9.20</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/net.sourceforge.htmlunit/htmlunit-core-js -->
    <dependency>
        <groupId>net.sourceforge.htmlunit</groupId>
        <artifactId>htmlunit-core-js</artifactId>
        <version>2.23</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/net.sourceforge.htmlunit/htmlunit -->
    <dependency>
        <groupId>net.sourceforge.htmlunit</groupId>
        <artifactId>htmlunit</artifactId>
        <version>2.23</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/net.sourceforge.htmlunit/neko-htmlunit -->
    <dependency>
        <groupId>net.sourceforge.htmlunit</groupId>
        <artifactId>neko-htmlunit</artifactId>
        <version>2.23</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.5.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-io -->
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-io</artifactId>
        <version>9.2.15.v20160210</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-util -->
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-util</artifactId>
        <version>9.2.15.v20160210</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.eclipse.jetty.websocket/websocket-api -->
    <dependency>
        <groupId>org.eclipse.jetty.websocket</groupId>
        <artifactId>websocket-api</artifactId>
        <version>9.2.15.v20160210</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.eclipse.jetty.websocket/websocket-client -->
    <dependency>
        <groupId>org.eclipse.jetty.websocket</groupId>
        <artifactId>websocket-client</artifactId>
        <version>9.2.15.v20160210</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.eclipse.jetty.websocket/websocket-common -->
    <dependency>
        <groupId>org.eclipse.jetty.websocket</groupId>
        <artifactId>websocket-common</artifactId>
        <version>9.2.15.v20160210</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/htmlunit-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>htmlunit-driver</artifactId>
        <version>2.23</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/jetty-repacked -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>jetty-repacked</artifactId>
        <version>9.2.13.v20160825</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-api -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-api</artifactId>
        <version>3.8.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>3.0.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-edge-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-edge-driver</artifactId>
        <version>3.0.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-firefox-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>3.8.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-ie-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-ie-driver</artifactId>
        <version>3.8.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.8.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-opera-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-opera-driver</artifactId>
        <version>3.8.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-remote-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-remote-driver</artifactId>
        <version>3.8.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-safari-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-safari-driver</artifactId>
        <version>3.0.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-support -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-support</artifactId>
        <version>3.8.1</version>
    </dependency>

Non fa ancora nulla:

> C:\Users\sstaple\eclipse-workspace\MySel20Proj>mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MySel20Proj 1.0
[INFO] ------------------------------------------------------------------------
Downloading from central: https://repo.maven.apache.org/maven2/com/google/guava/guava/21.0/guava-21.0.jar
Downloaded from central: https://repo.maven.apache.org/maven2/com/google/guava/guava/21.0/guava-21.0.jar (2.5 MB at 2.6 MB/s)
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MySel20Proj ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ MySel20Proj ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MySel20Proj ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ MySel20Proj ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\sstaple\eclipse-workspace\MySel20Proj\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.20.1:test (default-test) @ MySel20Proj ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running Selenium2Example
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.008 s - in Selenium2Example
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MySel20Proj ---
[INFO] Building jar: C:\Users\sstaple\eclipse-workspace\MySel20Proj\target\MySel20Proj-1.0.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ MySel20Proj ---
[INFO] Installing C:\Users\sstaple\eclipse-workspace\MySel20Proj\target\MySel20Proj-1.0.jar to C:\Users\sstaple\.m2\repository\MySel20Proj\MySel20Proj\1.0\MySel20Proj-1.0.jar
[INFO] Installing C:\Users\sstaple\eclipse-workspace\MySel20Proj\pom.xml to C:\Users\sstaple\.m2\repository\MySel20Proj\MySel20Proj\1.0\MySel20Proj-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.032 s
[INFO] Finished at: 2017-12-11T13:56:34Z
[INFO] Final Memory: 19M/63M
[INFO] ------------------------------------------------------------------------
Author: Steve Staple, 2017-12-08

1 answers

Come per il blocco di codice che hai condiviso tutto sembra a posto. Ma ancora una volta come hai detto org.seleniumhq.selenium selenium-server 3.0.1 è necessario scaricare obbligatoriogeckodriver binario da questo site e inseriscilo nel tuo sistema. Successivamente nel tuo blocco di codice devi menzionare come segue :

System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();

Fasi di esecuzione:

  1. Salva il Selenium2Example.java e il pom.xml file
  2. Nella Package Explorer, Right Click su pom.xml, seleziona Run AS e selezionare Maven clean
  3. Nella Package Explorer, Right Click su pom.xml, seleziona Run AS e selezionare Maven install
  4. Nella Package Explorer, Right Click su pom.xml, seleziona Run AS e selezionare Maven test

Aggiornamento 1

Come stai vedendo l'errore come 'selection does not contain a main type', assicurarsi che il file .java sia all'interno \ProjectSpace\src\test\java\


Aggiornamento 2

Come te vedi ancora No sources to compile, come ultima risorsa, Clean tutto il tuo Projects all'interno del tuo IDE, Close il IDE, Delete la cartella m2 (MAVEN_HOME) dal sistema forzatamente. Prendi un System Reboot. prova a Execute tu Test. Inoltre dalla riga di comando anche tu puoi fare un mvn clean, mvn install e mvn test

 1
Author: DebanjanB, 2017-12-08 16:46:01