简体   繁体   English

Selenium Java - 如何从不同的类调用一个类

[英]Selenium Java - How to call a class from different class

I am new to Selenium and I need help whatever I can get.我是 Selenium 的新手,我需要任何我能得到的帮助。 I will try to provide detailed info as much as I can.我会尽量提供详细的信息。 I need to call the object imgBtn01 or imgBtn02 inside the FIRST Class ( ImagesRepo ) from the SECOND Class.我需要从 SECOND 类调用 FIRST 类 ( ImagesRepo ) 内的对象imgBtn01imgBtn02 The reason I want to separate it, I want all to store all the image objects from a different class.我想把它分开的原因,我希望所有的图像对象都来自不同的类。

Selenium + Sikuli > Java > Maven Project Selenium + Sikuli > Java > Maven 项目

First Class from different Package不同套餐的头等舱

public class ImagesRepo {

    public void imageRepoApp() {  

    //Images assigning object
     Screen screen = new Screen();
     Pattern imgBtn01 = new Pattern("/Images/Btn_ButtonName01.png");
     Pattern imgBtn02 = new Pattern("/Images/Btn_ButtonName02.png");
}

Second class, from a different package:第二类,来自不同的包:

public class testBed {


    public static void callRepoImages() throws FindFailed {
        ReporImages();
    }


    @Test       
    public static void ReporImages() {
        Screen screen = new Screen();
        screen.click(imgBtn01); //the imgBtn01 has a redline
        screen.click(imgBtn02); //the imgBtn02 has a redline
        return;
    }
}

This looks to be more of a how to code in java type question.这看起来更像是如何在 java 类型问题中编码。

One way to do this is to create public variables to your first class and fetch these from the second class.一种方法是为您的第一个类创建公共变量并从第二个类中获取这些变量。

Change 1st class to something like;将 1st class 更改为类似的内容;

public class ImagesRepo {

    public Pattern imgBtn01;
    public Pattern imgBtn02;

public void imageRepoApp() {  

//Images assigning object
 Screen screen = new Screen();
 imgBtn01 = new Pattern("/Images/Btn_ButtonName01.png");
 imgBtn02 = new Pattern("/Images/Btn_ButtonName02.png");
}

You can then get these public variables in Second class as;然后,您可以在 Second class 中获取这些公共变量;

public class testBed{


    public static void callRepoImages() throws FindFailed {
        ReporImages();
    }


@Test       
public static void ReporImages() {
    ImagesRepo imgrepo = new ImagesRepo();
    imgrepo.imageRepoApp();   //So that pattern assignment is done.
    Screen screen = new Screen();
    screen.click(imgrepo.imgBtn01); //the imgBtn01 has a redline
    screen.click(imgrepo.imgBtn02); //the imgBtn02 has a redline
    return;

}
}

Also, add import for the class ImagesRepo appropriately in the class testBed此外,在类 testBed 中适当地为类 ImageRepo 添加导入

Code untested.代码未经测试。

There are better ways to do it, but this seems the way to go with minimal changes to your code.有更好的方法可以做到这一点,但这似乎是对代码进行最少更改的方法。

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

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