0

I have two selenium elements where i need to click. But these elements are with a different id.

findElement(By.id(ID_1)).click();

findElement(By.id(ID_2)).click();

findElement(By.id(ID_1)).click() should be clicked if the Class_1 is executed, if Class_2 is called, then findElement(By.id(ID_2)).click() should be clicked.

I am trying to avoid duplicate code because these above 2 find elements are inside one method which is called Class_1 and Class_2.

I tried something like this but it didn't solve my issue

  if (Class_1.class)
        {
            findElement(By.id(ID_1)).click();
        }
    
  else if (Class_2.class)
        {
            findElement(By.id(ID_2)).click();
        }

I am getting "Type mismatch: cannot convert from Class<Class_1> to boolean". I mean I am trying to do like this. If this is class_1, then run this, else run else if part.

Thanks for any guidance in advance.

2
  • I did not get it, why do you want to differentiate based on class ? why not in a single class ? Commented Jul 2, 2021 at 10:51
  • @cruisepandey because these two classes refers to different IPs addresses to perform some tasks. and both classes access to this method. If i create two methods, then issue is solved but code will be duplicated. Commented Jul 2, 2021 at 10:53

1 Answer 1

1

You schouldn't separate your test into two (or more) different classes. Instead use one class and parametrize your method:

public void myTestMethod(String ipAdress) {
    if (ipAdress.equals("FOO_IP")) {
        findElement(By.id("FOO_ID")).click();
    }
    else if (ipAdress.equals("BAR_IP")) {
        findElement(By.id("BAR_ID")).click();
    }
    else {
        // code for uknown ip adress
    }
}

and simply call the method with desired argument:

myTestMethod("FOO_IP");
myTestMethod("BAR_IP");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.