如何为硒测试创建兼容的JAR文件? [英] How to Create a Compatible JAR File for Selenium Tests?

查看:52
本文介绍了如何为硒测试创建兼容的JAR文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个脚本,用于使用Eclipse中的Java在Selenium中对网站执行少量自动化测试。

I have created a script to perform few automated tests in Selenium for a website using Java in Eclipse.

我的目的是为自动化创建一个JAR文件测试,以便在简单执行文件时,测试将在配置有Selenium环境的任何其他系统上运行。为此,我通过单击文件菜单中的导出选项,从Eclipse创建了可运行的JAR文件。 JAR文件的名称是 Test MyWebsite.jar

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. For that I have created a runnable JAR file from Eclipse by clicking on the Export option from the File menu. The name of the JAR file is Test MyWebsite.jar.

我的Java类的源代码如下所示:

The source code of my Java classes is given below:

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

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

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
    }
}

这里的问题是,尽管使用Selenium Webdriver在其他计算机上设置并配置了环境,但仍复制了 Test MyWebsite.jar 在第三方计算机上并通过双击运行该文件,则测试无法成功运行。 Chrome浏览器会暂时打开,然后关闭,而没有打开 MyWebsite的URL。换句话说,仅执行 Main.java 类中的两行代码。 JAR文件无法在Eclipse项目中找到其他两个关联的类。但是,当我在计算机上执行文件时,测试可以完美运行,这意味着我创建JAR文件的方式存在问题。请注意,这些只是我在Eclipse项目中的5个不同包中创建的12个类中的三个类,以便于理解。

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. What happens is the Chrome browser opens momentarily and then closes without opening the 'MyWebsite's' URL. In other words, only the two lines of code in the Main.java class are executed. The JAR file is unable to find the other two associated classes in the Eclipse project. 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. 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.

有人可以告诉我我在哪里我在创建将在我的计算机以及配置了Selenium环境的另一台计算机上运行的JAR文件时出错了吗?作为Selenium的初学者,我遵循这些说明。

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? As a beginner in Selenium I had followed these instructions while creating my Selenium project in Eclipse. Replies at the earliest will be highly appreciated.

谢谢。

推荐答案

好吧,我自己找到了解决问题的方法。

Okay I found out the solution to the problem myself.

问题在于我在Eclipse中创建Runnable JAR文件的方式。我在第一页的库处理标签下选择了将所需的库提取到生成的JAR中选项(默认情况下由Eclipse选择)。这样做并没有将所需的外部Selenium,TestNG和JRE系统库,JAR文件库打包到我的可运行JAR中,因此它无法找到所需的类。选择第二个选项将所需的库打包到生成的JAR中后,我的JAR开始完美运行。

The problem lied in the way I was creating the Runnable JAR file in Eclipse. 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. 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. My JAR started working perfectly after selecting the second option, Package required libraries into generated JAR.

这篇关于如何为硒测试创建兼容的JAR文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆