Selenium Java - How to call a class from different class - java

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.

Related

Accessing a particular instance of class inside the same class in java

I have a java code with the structure that is shown below:
public class x{
public static void main(string[] args)
{
ysample1 = new y(m)
ysample2 = new y(l)
....
}
}
public class y{
private int m_m
public y(int m)
{
m_m = m
}
public void control()
{
h h1 = new h(ysample2)
}
}
At some point when I want to call method control for ysample1 I may need to access ysample2 object.How can I define instance of class y global, so I can access ysample2 inside the control method in class y?
Does anyone know how can I fix this? Thanks.
You can't do what you want to do the way you wrote it.
I think you need to ridefine "control()" method like this:
public void control(Y ysample)
{
h h1 = new h(ysample)
}
So now you need to have an "ysample" as parameter and you can do from your main
control(ysample2);
and you will have what i understood from you question. If you need something else please comment.

Cant access methods from another class

Im calling DataComparison()
public class SteganographyGUI {
...
DataComparison dataComp;
dataComp = new DataComparison();
}
public int getLSB(){
String x = fileChooser1.getSelectedFile().getAbsolutePath();
x = x.substring(x.length() - 10, x.length() - 9);
return Integer.parseInt(x);
}
when some criteria are met. My problem is that, when I try to access getLSB by using gui.getLSB()
public class DataComparison {
public static SteganographyGUI gui;
...
public DataComparison(){
lsb = gui.getLSB();
}
public static void main(String[] args) {
gui = new SteganographyGUI();
gui.setVisible(true);
}
Error appears - Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
How can I fix this?
You are trying to call getLSB() in your DataComparison class, but you don't give it the reference of your SteganographyGUI class. So change the following line:
DataComparison dataComp;
dataComp = new DataComparison();
to:
DataComparison dataComp;
dataComp = new DataComparison(this);
And change the constructor as well:
public DataComparison(SteganographyGUI guiRef){
gui = guiRef;
}
You are doing cyclic calls in your code. Try another class just for the main and call its methods in order:
class 1: `public class SteganographyGUI {...}`
class 2: `public class DataComparison {...}
class 3:
`public class XXXX {
public static void main(String[] args) {
gui = new SteganographyGUI();
gui.setVisible(true);
}
}`
I have helped :)
When you try to initiate static "gui" variable
gui = new SteganographyGUI();
you execute SteganographyGUI class constructor which (probably) calls DataComparison class constructor
public DataComparison(){
lsb = gui.getLSB();
}
which uses static "gui" variable which is not set yet. This is the reason you got "java.lang.NullPointerException".
Yes, I know "the exception message is misleading" :)

Get value from different class inside an event

I have two classes:
public jComboBox() {
... // this is a autocomplete jComboBox btw
...
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie) {
if(ie.getStateChange() == 1) {
String selectedItem = (String)getSelectedItem();
randomMethod(selectedItem);
}
}
});
}
private void randomMethod(String selectedItem){
someClass sc = new someClass();
String randomString = selectedItem;
sc.getRandomString(randomString);
}
and
public someClass() {
...
...
}
public void getRandomString(String randomString){
defaultTableModel.setRowCount(0);
.. do-something ..
}
Is this method fine? If not, I need some alternative on this one, because i'm having problems for example, using defaultTableModel.setRowCount(0) because the table wont empty, not unless I put the setRowCount(0) on other methods inside someClass class.
Basic java access specifier stuff..... how are you calling this private method getRandomString from randomMethod()? the visibility of private method of a class is only the class, not anywhere else. Therefore, your following code:
private void randomMethod(String selectedItem){
someClass sc = new someClass();
String randomString = selectedItem;
fs.getRandomString(randomString); // This will not work
}
is not going to work because of the access specifier private. If you can allow the access rights to be specific to the package you have, you can change it to:
protected void getRandromString(String randromString) {...}
Just to demonstrate what I mean:
package com.stackoverflow.solutionmaker;
public class Aclass {
public Aclass(){
somePrivMethod();
}
public void aMethod(){
System.out.println("Can see me from anywehre bcoz I am public");
}
private void somePrivMethod(){
System.out.println("Cannot find me from anywhere because I am private t Aclass");
}
}
Now the runner class:
package com.stackoverflow.solutionmaker;
public class StackOverflowSolutionsRunner {
public static void main(String[] args) {
Aclass aClass = new Aclass(); // It will display"Cannot find me from anywhere because I am private t Aclass"
aClass.aMethod(); // It will display "Can see me from anywehre bcoz I am public
aClass.somePrivMethod(); // Will throw a compile-time error
}
}
A good exercise for you now to compile these two from command line and see what error message you get. Alternatively, using Eclipse smart IDE or Jcreator, you can see that your private access specifier is causing red messages to appear.

How can I access a Local Variable which is an object from within another class? - Java

My understanding is that you can't do what I'm asking. If you look at the starred (*) commented errors in the code below, you can see what I'm trying to access. I feel like I need to be able to do this so that I can use a method to dynamically create many objects and then access all of those objects from other objects.
Is there a way to do this that I'm missing, or am I just messing something up? If not, how should I go about doing this to enable me to get the same functionality as below? If there's any way to do this other than passing the objects around, it would be appreciated (passing objects seems like so much work - especially with multi-dimensional arrays of objects - there should be an easy way to instantiate package-private objects that can be accessed anywhere else in the package). But if passing is the only way, please let me know the best way to do it, especially when I'm passing a two-dimensional array of a bunch of objects. Thanks!
package simpleclasswithinclasstest;
class Game {
static int boardSize;
Engine gameEngine;
Game() {
}
public void run() {
gameEngine = new Engine();
gameEngine.play();
}
public int getBoardSize() {
return boardSize;
}
}
class Engine {
int boardSize;
Engine() {
}
public void play() {
this.boardSize = currentGame.getBoardSize(); // *****1 Error is here.
// *****It doesn't recognize currentGame, but I want it to.
}
void doNothing() {
}
}
class Board {
Board() {
}
void Test() {
gameEngine.doNothing(); // 2 *****Error is here.
// *****It doesn't recognize gameEngine.
}
}
public class SimpleClassWithinClassTest {
static Game currentGame;
public static void main(String[] args) {
currentGame = new Game();
currentGame.run();
}
}
You will get access to gameEngine through your Board class by passing it as a parameter to Board. When you instantiate your Board, you could do something like this:
class Engine {
int boardSize;
Engine () {
Board board = new Board(this);
}
public void play() {
}
void doNothing() {
// magic stuff in here
}
}
class Board {
Engine engine;
Board (Engine gameEngine) {
this.engine = gameEngine
}
void Test() {
engine.doNothing(); // No error here :-) and this engine is your main one
}
}
Take a look at the concept of message-driven communication. Things might get clearer for you by reading this answer.
In the following picture, which I took from the answer linked above, you can imagine f as your engine object within the Engine class, and c as your engine within the Board class. You are actually manipulating the same object.
As for your other problem (the first one): it can't recognize currentGame because you don't have any variable with that name in your scope.
There is no variable of any type named 'currentGame' in scope at that point in the code.
Furthermore, while boardSize is a static package-protected variable, the method getBoardSize() is an instance variable. One possible solution is to make the method static and package protected, then you can do this:
public void play() {
this.boardSize = Game.getBoardSize();
}
This is like initializing an int variable in 1 function and trying to access it from another function.The objects(you are trying to access) are out of scope in the part of the code where you are trying to access.You can resolve this issue by sending this
as a parameter and recieving it as an object in the corresponding method.
We can use class reference to call static methods only. So you can make the play a static method.
class Engine {
int boardSize;
Engine() {
}
public void play() {
this.boardSize = currentGame.getBoardSize(); // *****1 Error is here.
// *****It doesn't recognize currentGame, but I want it to.
}
static void doNothing() {
}
}
class Board {
Board() {
}
void Test() {
Engine.doNothing();
}
}
The other way is to make an Object from the class and access the non static methods within that object.
class Engine {
int boardSize;
Engine() {
}
public void play() {
this.boardSize = currentGame.getBoardSize(); // *****1 Error is here.
// *****It doesn't recognize currentGame, but I want it to.
}
void doNothing() {
}
}
class Board {
Board() {
}
void Test() {
Engine gameEngine = new Engine();
gameEngine.doNothing();
}
}

java.lang.NullPointerException when filling an array of objects

There's something I'm missing here. with this code I get a java.lang.NullPointerException:
public static void main(String[] args) {
Board board = new Board();
board.Initialise();
}
public class Board {
private Obj[][] tableau;
public void Board() {
tableau = new Obj[8][8];
}
public void Fill_Board() {
tableau[0][0]= new Obj('B');
}
}
But with this other code I get no error. What I am doing wrong, and how to initialize properly this array of object?
public class Board {
private Obj[][] tableau = new Obj[8][8];
public void Board() {
}
public void Fill_Board() {
tableau[0][0]= new Obj('B');
}
}
Board() ends up being a member function and not a constructor, and therefore never gets called. The problem is the void keyword, which needs to be removed:
public Board() { /* removed the `void' */
tableau = new Obj[8][8];
}
I get no error when I run this code (changing Obj to an actual class), perhaps you can provide a more concrete example with a main method of what you're trying to do?
If you're looking at making a constructor then it needs to be the same name as your class, i.e. Tab and have no return type.
So you would need to add:
public Tab() {
// initialization code here
}
You're constructor will run whenever you create a new instance of that class. You want to use it to initialize all of your variables.
Tab t = new Tab(); // Constructor runs
Edit:
You're main method uses class Board but you have given us a class called Tab. I can assume that you really want class Board so you should change all Tab to Board in the above example, if that's what you're looking for.

Categories

Resources