2012-08-30 75 views
0

我創建了pom.xml和一個示例文件Selenium2Example.java來設置Maven。 我也跟着在這裏http://seleniumhq.org/docs/03_webdriver.html#chrome-driverSelenium WebDriver第一個Java示例設置

但我得到的錯誤說明書上運行Selenium2Example.java爲Java應用程序「選擇不包含主型」

這裏是我使用

<?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-java</artifactId> 
       <version>2.25.0</version> 
      </dependency> 
      <dependency> 
       <groupId>com.opera</groupId> 
       <artifactId>operadriver</artifactId> 
      </dependency> 
     </dependencies> 
     <dependencyManagement> 
      <dependencies> 
       <dependency> 
        <groupId>com.opera</groupId> 
        <artifactId>operadriver</artifactId> 
        <version>0.16</version> 
        <exclusions> 
         <exclusion> 
          <groupId>org.seleniumhq.selenium</groupId> 
          <artifactId>selenium-remote-driver</artifactId> 
         </exclusion> 
        </exclusions> 
       </dependency> 
      </dependencies> 
     </dependencyManagement> 
</project> 

這裏的pom.xml是我使用的Selenium2Example.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.Chrome.ChromeDriver; 
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 Chrome driver 
     // Notice that the remainder of the code relies on the interface, 
     // not the implementation. 
     WebDriver driver = new ChromeDriver(); 
     // 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(); 
    } 
} 

在運行它提供的以下錯誤 運行 - >運行(在TestNG下選擇Selenium2Example.java

錯誤是

org.testng.TestNGException: 
Cannot find class in classpath: Selenium2Example 
    at org.testng.xml.XmlClass.loadClass(XmlClass.java:76) 
    at org.testng.xml.XmlClass.init(XmlClass.java:68) 
    at org.testng.xml.XmlClass.<init>(XmlClass.java:54) 
    at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:542) 
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506) 
    at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182) 
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(XMLDTDValidator.java:766) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1302) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2715) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764) 
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123) 
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210) 
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568) 
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:302) 
    at javax.xml.parsers.SAXParser.parse(SAXParser.java:195) 
    at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:17) 
    at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:10) 
    at org.testng.xml.Parser.parse(Parser.java:172) 
    at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:310) 
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:88) 
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) 
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) 

回答

2

首先,你做了正確的決定(從我的觀點)使用Maven,Java和webdriver的;)

你所做的,就像你選擇了錯誤的包或文件運行看!

這不是Selenium的問題,這是您的運行配置問題,您的IDE!

我猜你使用Eclipse?

嘗試用鼠標右鍵單擊您要運行的文件 - >運行爲 - > Java應用程序

這應該解決您的問題...

如果你想要做一些測試與硒的webdriver,不要忘記選擇一個測試框架。我會推薦JUnit甚至更好的TestNG ...

+0

我只是這樣做。 看看錯誤信息。 – R11G

+0

「選擇不包含主類型」,並且TestNG錯誤是不同類型的錯誤....如果您使用主運行它,則不需要TestNG-如果使用TestNG運行它,則必須刪除main和用@Test註釋它。使用此[手動](http://testng.org/doc/selenium.html) –

+0

TestNG比JUnit更好...我認爲 –

0

java代碼沒有問題。檢查安裝正確的驅動程序..嘗試通過更改鉻驅動程序與Firefox驅動程序。

相關問題