Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java8 with Selenium WebDriver

I am new to Java-8 and appium and trying to write my webdriver code in Java-8 as mentioned below

Currently below code is working fine but I don't want to write Function and Consumer in separate lines as mentioned below in lines line 5 and line 6 and i want to optimize the code more i.e something like Function within Consumer or Consumer within Function.Thanks! in Advance

public class Gestures_Drag_And_Drop {

     AndroidDriver<AndroidElement> driver;

     Function<String, AndroidElement> F = driver::findElementByXPath;//line 5
     Consumer<AndroidElement> C = AndroidElement::click;  //line 6

     Gestures_Drag_And_Drop() {
         C.accept(F.apply("//android.widget.TextView[@text='Views']"));
     }

     public static void main(String[] args) throws MalformedURLException {
         new Gestures_Drag_And_Drop();
     }
}
like image 896
Ketan Sethi Avatar asked Jan 28 '26 13:01

Ketan Sethi


1 Answers

Your function or consumer variable not need to be just method reference, you can define whatever method body you want using lambda expressions. I guess you asking for something like this:

BiConsumer<String, WebDriver> findAndClick = (locator, webdriver) -> webdriver.findElement(By.xpath(locator)).click();
like image 177
Mihail Avatar answered Jan 31 '26 01:01

Mihail