简体   繁体   中英

Selenium Java - How to call a class from different class

I am new to Selenium and I need help whatever I can get. 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. 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

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.

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;

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;

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

Code untested.

There are better ways to do it, but this seems the way to go with minimal changes to your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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