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.
Advertisement
Answer
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");