【问题标题】:Eclipse error: 'Selection does not contain a main type/an applet'Eclipse 错误:“选择不包含主要类型/小程序”
【发布时间】:2013-03-28 10:16:17
【问题描述】:

我知道已经有非常类似的问题,但是我是 Java/Selenium WebDriver 的初学者,最初是在 Selenium IDE 中创建的,然后将其导出为 Java / JUnit4 / WebDriver 和希望得到具体的帮助,因为我不确定我到底哪里出错了。当我作为 Java Application 运行时,我收到错误 Selection 不包含主要类型,当我作为 Java Applet 运行时,我收到错误 em>选择不包含小程序。这是我的代码:

package com.exports.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Login {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "https://www.example.co.uk/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testLogin() throws Exception {
    driver.get(baseUrl + "/123example/index.jsp");
    // ERROR: Caught exception [ERROR: Unsupported command [getEval | prompt("Username: ") | ]]
    driver.findElement(By.name("LoginText1")).clear();
    element.sendKeys(user input);
    //driver.findElement(By.name("LoginText1")).clear();
    //driver.findElement(By.name("LoginText1")).sendKeys(_username);
    // ERROR: Caught exception [ERROR: Unsupported command [getEval | prompt("Password: ") | ]]
    driver.findElement(By.name("password1")).clear();
    element.sendKeys(user input);
    //driver.findElement(By.name("password1")).clear();
    //driver.findElement(By.name("password1")).sendKeys(_password);
    driver.findElement(By.cssSelector("img[alt=\"Login\"]")).click();
    // ERROR: Caught exception [ERROR: Unsupported command [windowFocus |  | ]]
    driver.switchTo().window("CarrierNet Desktop");
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alert.getText();
    } finally {
      acceptNextAlert = true;
    }
  }
}

非常感谢任何帮助。

【问题讨论】:

  • 好吧,要运行它,它应该包含 public static void main(String args[]) 它没有的方法。要“运行”它,您必须运行 TEST。但我不知道如何在 Eclipse 中做到这一点
  • @PavelJanicek 我会在哪里添加,public static void main(String args[])
  • @Perception 您如何将它作为 jUnit 测试运行,因为当我尝试这样做时,它要求配置它,但我不知道该放什么。
  • @D.Shah - 你是说 Eclipse 要求你配置 jUnit?你用的是什么版本的 Eclipse?
  • @Perception 我正在为 Web 开发人员运行 Eclipse Java EE IDE。版本:Juno Service Release 2。内部版本 ID:20130225-0426。几天前我从网站上下载了它。

标签: java eclipse selenium selenium-webdriver


【解决方案1】:

决定将其作为 java 应用程序运行。

将其作为 JUNIT 测试运行。

【讨论】:

  • 我现在设法做到了,但收到错误“未找到 JUnit 测试”。
【解决方案2】:

扩展 HemChe 的答案(这让我走上了正轨):

将 JUnit 和 Selenium .jar 文件位置添加到您的类路径中,然后设置您的网络驱动程序的路径(以便 Selenium 知道如何控制浏览器),然后以您的类作为参数调用 JUnit 运行器。这就是它在我的 linux 命令行上的样子

cd /path/to/my/compiled/class
java -classpath .:/path/to/junit/classes/*:/path/to/selenium/classes/* -Dwebdriver.gecko.driver=/path/to/geckodriver org.junit.runner.JUnitCore MyTestClass

就像魔术一样,Firefox 窗口打开并运行您的测试,控制台中出现错误。

Selenium 类路径应该有文件 client-combined-x.x.x-nodeps.jar JUnit 类路径应该有很多文件,包括 junit-x.xx.jar

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-18
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多