简体   繁体   English

如何为硒测试创建兼容的JAR文件?

[英]How to Create a Compatible JAR File for Selenium Tests?

I have created a script to perform few automated tests in Selenium for a website using Java in Eclipse. 我创建了一个脚本,用于在Eclipse中使用Java在Selenium中为网站执行少量自动化测试。

My aim here is to create a JAR file for my automated tests so that when the file is simply executed, the tests will run on any other system configured with a Selenium environment. 我的目的是为自动化测试创建一个JAR文件,以便仅执行该文件时,这些测试即可在配置有Selenium环境的任何其他系统上运行。 For that I have created a runnable JAR file from Eclipse by clicking on the Export option from the File menu. 为此,我通过单击“文件”菜单中的“导出”选项,从Eclipse创建了可运行的JAR文件。 The name of the JAR file is Test MyWebsite.jar . JAR文件的名称是Test MyWebsite.jar

The source code of my Java classes is given below: 我的Java类的源代码如下:

Main.java Main.java

package testproject.main;

import testproject.testmywebsite.*;


public class Main {

    public static void main(String[] args) {
        TestMyWebsite tmw = new TestMyWebsite();
        tmw.testMyWebsite();
        tmw.stopTest();
    }
}

TestMyWebsite.java TestMyWebsite.java

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.*;
import testproject.testmywebsite.tools.*;
import testproject.testmywebsite.login.*;

public class TestMyWebsite {

    private WebDriver driver;

    public TestMyWebsite() {
        setUp();
    }

    private void setUp() {
        // Create a new instance of the Chrome driver
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    public void testMyWebsite() {
        testLogin();
    }

    public void testLogin() {
        TestLogin tl = new TestLogin(driver);
        tl.testLogin();
    }

    public void stopTest() {
        //Close the browser
        driver.quit();
    }
}

TestLogin.java TestLogin.java

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestLogin {

    private WebDriver;

    public TestLogin(WebDriver driver) {
        this.driver = driver;
    }

    public void testLogin() {
        //Perform the login test
    }
}

The problem here is that despite setting up and configuring the environment on other computers with the Selenium webdriver, copying the Test MyWebsite.jar in the third-party computer and running the file by double-clicking it, the tests do not run successfully. 这里的问题是,尽管使用Selenium Test MyWebsite.jar在其他计算机上设置并配置了环境,但在第三方计算机中复制了Test MyWebsite.jar并通过双击运行该文件,但测试仍无法成功运行。 What happens is the Chrome browser opens momentarily and then closes without opening the 'MyWebsite's' URL. Chrome浏览器会暂时打开,然后关闭,而没有打开“ MyWebsite's” URL。 In other words, only the two lines of code in the Main.java class are executed. 换句话说,仅执行Main.java类中的两行代码。 The JAR file is unable to find the other two associated classes in the Eclipse project. JAR文件无法在Eclipse项目中找到其他两个关联的类。 However when I execute the file in my computer the tests are perfectly run, which means that there is a problem with how I have created the JAR file. 但是,当我在计算机上执行文件时,测试可以完美运行,这意味着我创建JAR文件的方式存在问题。 Please note that these are just three classes of the 12 classes I have created in 5 different packages in my Eclipse project to make understanding easier. 请注意,这些只是我在Eclipse项目中通过5个不同的包创建的12个类中的三个类,以使理解更加容易。

Can anyone please tell me where am I going wrong in creating my JAR file which will run both on my computer as well as on another computer configured with the Selenium environment? 谁能告诉我在创建将在我的计算机以及配置了Selenium环境的另一台计算机上运行的JAR文件时我要怎么做? As a beginner in Selenium I had followed these instructions while creating my Selenium project in Eclipse. 作为Selenium的初学者,我在Eclipse中创建Selenium项目时遵循了这些说明。 Replies at the earliest will be highly appreciated. 最早的答复将不胜感激。

Thank you. 谢谢。

Okay I found out the solution to the problem myself. 好吧,我自己找到了解决问题的办法。

The problem lied in the way I was creating the Runnable JAR file in Eclipse. 问题在于我在Eclipse中创建Runnable JAR文件的方式。 I was selecting the Extract required libraries into generated JAR option (selected by Eclipse by default) under the Library handling label on the first page. 我正在第一页的“库处理”标签下选择“将所需的库提取到生成的JAR中”选项(默认情况下由Eclipse选择)。 By doing so it was not packing the required external Selenium, TestNG and JRE System Library, JAR file libraries in my runnable JAR, hence it was not able to find the required classes. 这样做并没有在我的可运行JAR中打包所需的外部Selenium,TestNG和JRE系统库,JAR文件库,因此无法找到所需的类。 My JAR started working perfectly after selecting the second option, Package required libraries into generated JAR. 选择第二个选项“将所需的库打包到生成的JAR中”后,我的JAR开始完美运行。

For one thing, you should use a test runner, such as JUnit or TestNG, to run your tests. 一方面,您应该使用测试运行器(例如JUnit或TestNG)来运行测试。 Another thing is to make sure you are using Maven and conforming to its ' standard directory layout '. 另一件事是确保您正在使用Maven并遵守其“ 标准目录布局 ”。 By using Maven, you will be able to run the 'install' or 'package' goal/task to create the standalone 'executable jar'. 通过使用Maven,您将能够运行“安装”或“包”目标/任务来创建独立的“可执行jar”。 Keep in mind that you will need to configure Maven in a unusual way , where you tell it to include all files under 'src/main/java' and 'src/test/java' into the same single .jar file. 请记住,您将需要以一种不寻常的方式来配置Maven,在该方式下,您需要告诉它将'src / main / java'和'src / test / java'下的所有文件都包含在同一个.jar文件中。 Also, you will need to configure Maven in such a way as to create a .jar that is actually executable . 同样,您将需要以创建实际可执行的.jar的方式配置Maven。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM