Best Winium code snippet using org.openqa.selenium.winium.WiniumDriverService
Source:WiniumDriverService.java  
...17 * import org.junit.*;18 * import org.junit.runner.RunWith;19 * import org.junit.runners.JUnit4;20 * import org.openqa.selenium.winium.WiniumDriver;21 * import org.openqa.selenium.winium.WiniumDriverService;22 * import org.openqa.selenium.winium.DesktopOptions;23 *24 * {@literal @RunWith(JUnit4.class)}25 * public class WiniumTest extends TestCase {26 *     private static WiniumDriverService service;27 *     private WebDriver driver;28 *29 *     {@literal @BeforeClass}30 *     public static void createAndStartService() {31 *         service = new WiniumDriverService.Builder()32 *             .usingDriverExecutable("path_to_driver_executable")33 *             .usingAnyFreePort()34 *             .withVerbose(true)35 *             .withSilent(false);36 *             .buildDesktopService();37 *         service.start();38 *     }39 *40 *     {@literal @AfterClass}41 *     public static void createAndStopService() {42 *         service.stop();43 *     }44 *45 *     {@literal @Before}46 *     public void createDriver() {47 *         DesktopOptions options = DesktopOptions();48 *         options.setApplicationPath("C:\\Windows\\System32\\notepad.exe");49 *50 *         driver = new WiniumDriver(service, options);51 *     }52 *53 *     {@literal @After}54 *     public void quitDriver() {55 *         driver.quit();56 *     }57 *58 *     {@literal @Test}59 *     public void testSomething() {60 *         // Rest of test61 *     }62 * }63 * }</pre>64 */65public class WiniumDriverService extends DriverService {66    /**67     * Boolean system property that defines whether the WiniumDriver executable should be started68     * with verbose logging.69     */70    public static final String WINIUM_DRIVER_VERBOSE_LOG = "webdriver.winium.verbose";71    /**72     * Boolean system property that defines whether the WiniumDriver executable should be started73     * without any output.74     */75    public static final String WINIUM_DRIVER_SILENT = "webdriver.winium.silent";76    /**77     * System property that defines the location of the log that will be written by service78     */79    public static final String WINIUM_DRIVER_LOG_PATH_PROPERTY = "webdriver.winium.logpath";80    /**81     * Creates a default instance of the WiniumDriverService using a default path to the Winium Desktop Driver.82     * @return A {@link WiniumDriverService} using Winium Desktop and random port83     */84    public static WiniumDriverService createDesktopService() {85        return new Builder().usingAnyFreePort().buildDesktopService();86    }87    /**88     * Creates a default instance of the WiniumDriverService using a default path to the Winium WindowsPhone Driver.89     * @return A {@link WiniumDriverService} using Winium WindowsPhone and random port90     */91    public static WiniumDriverService createSilverlightService() {92        return new Builder().usingAnyFreePort().buildSilverlightService();93    }94    /**95     * Creates a default instance of the WiniumDriverService using a default path to the Winium StoreApps Driver.96     * @return A {@link WiniumDriverService} using Winium StoreApps and random port97     */98    public static WiniumDriverService createStoreAppsService() {99        return new Builder().usingAnyFreePort().buildStoreAppsService();100    }101    protected WiniumDriverService(File executable, int port, ImmutableList<String> args,102                                  ImmutableMap<String, String> environment) throws IOException {103        super(executable, port, args, environment);104    }105    public static class Builder extends DriverService.Builder<WiniumDriverService, WiniumDriverService.Builder> {106        private static final String DESKTOP_DRIVER_SERVICE_FILENAME = "Winium.Desktop.Driver.exe";107        private static final String SILVERLIGHT_DRIVER_SERVICE_FILENAME = "WindowsPhoneDriver.OuterDriver.exe";108        private static final String STORE_APPS_DRIVER_SERVICE_FILENAME = "Winium.StoreApps.Driver.exe";109        private static final String DESKTOP_DRIVER_EXE_PROPERTY = "webdriver.winium.driver.desktop";110        private static final String SILVERLIGHT_DRIVER_EXE_PROPERTY = "webdriver.winium.driver.silverlight";111        private static final String STORE_APPS_DRIVER_EXE_PROPERTY = "webdriver.winium.driver.storeaps";112        private static final String DESKTOP_DRIVER_DOCS_URL = "https://github.com/2gis/Winium.Desktop";113        private static final String SILVERLIGHT_DRIVER_DOCS_URL = "https://github.com/2gis/winphonedriver";114        private static final String STORE_APPS_DRIVER_DOCS_URL = "https://github.com/2gis/Winium.StoreApps";115        private static final String DESKTOP_DRIVER_DOWNLOAD_URL = "https://github.com/2gis/Winium.Desktop/releases";116        private static final String SILVERLIGHT_DRIVER_DOWNLOAD_URL = "https://github.com/2gis/winphonedriver/releases";117        private static final String STORE_APPS_DRIVER_DOWNLOAD_URL = "https://github.com/2gis/Winium.StoreApps/releases";118        private File exe = null;119        private boolean verbose = Boolean.getBoolean(WINIUM_DRIVER_VERBOSE_LOG);120        private boolean silent = Boolean.getBoolean(WINIUM_DRIVER_SILENT);121        /**122         * Sets which driver executable the builder will use.123         *124         * @param file The executable to use.125         * @return A self reference.126         */127        @Override128        public Builder usingDriverExecutable(File file) {129            checkNotNull(file);130            checkExecutable(file);131            this.exe = file;132            return this;133        }134        /**135         * Configures the driver server verbosity.136         *137         * @param verbose true for verbose output, false otherwise.138         * @return A self reference.139         */140        public Builder withVerbose(boolean verbose) {141            this.verbose = verbose;142            return this;143        }144        /**145         * Configures the driver server for silent output.146         *147         * @param silent true for silent output, false otherwise.148         * @return A self reference.149         */150        public Builder withSilent(boolean silent) {151            this.silent = silent;152            return this;153        }154        /**155         * Creates a new {@link WiniumDriverService} to manage the Winium Desktop Driver server.156         * Before creating a new service, the builder will find a port for the server to listen to.157         *158         * @return The new {@link WiniumDriverService} object.159         */160        public WiniumDriverService buildDesktopService() {161            int port = getPort();162            if (port == 0) {163                port = PortProber.findFreePort();164            }165            if (exe == null) {166                exe = findDesktopDriverExecutable();167            }168            try {169                return new WiniumDriverService(exe, port, createArgs(), ImmutableMap.<String, String>of());170            } catch (IOException e) {171                throw new WebDriverException(e);172            }173        }174        /**175         * Creates a new {@link WiniumDriverService} to manage the Winium WindowsPhone Driver server.176         * Before creating a new service, the builder will find a port for the server to listen to.177         *178         * @return The new {@link WiniumDriverService} object.179         */180        public WiniumDriverService buildSilverlightService() {181            int port = getPort();182            if (port == 0) {183                port = PortProber.findFreePort();184            }185            if (exe == null) {186                exe = findSilverlightDriverExecutable();187            }188            try {189                return new WiniumDriverService(exe, port, createArgs(), ImmutableMap.<String, String>of());190            } catch (IOException e) {191                throw new WebDriverException(e);192            }193        }194        /**195         * Creates a new {@link WiniumDriverService} to manage the Winium StoreApps Driver server.196         * Before creating a new service, the builder will find a port for the server to listen to.197         *198         * @return The new {@link WiniumDriverService} object.199         */200        public WiniumDriverService buildStoreAppsService() {201            int port = getPort();202            if (port == 0) {203                port = PortProber.findFreePort();204            }205            if (exe == null) {206                exe = findStoreAppsDriverExecutable();207            }208            try {209                return new WiniumDriverService(exe, port, createArgs(), ImmutableMap.<String, String>of());210            } catch (IOException e) {211                throw new WebDriverException(e);212            }213        }214        @Override215        protected File findDefaultExecutable() {216            return findDesktopDriverExecutable();217        }218        @Override219        protected ImmutableList<String> createArgs() {220            if (getLogFile() == null) {221                String logFilePath = System.getProperty(WINIUM_DRIVER_LOG_PATH_PROPERTY);222                if (logFilePath != null) {223                    withLogFile(new File(logFilePath));224                }225            }226            ImmutableList.Builder<String> argsBuidler = new ImmutableList.Builder<String>();227            if (silent) {228                argsBuidler.add("--silent");229            }230            if (verbose) {231                argsBuidler.add("--verbose");232            }233            if (getLogFile() != null) {234                argsBuidler.add(String.format("--log-path=%s", getLogFile().getAbsolutePath()));235            }236            return argsBuidler.build();237        }238        @Override239        protected WiniumDriverService createDriverService(File exe, int port, ImmutableList<String> args,240                                                          ImmutableMap<String, String> environment) {241            try {242                return new WiniumDriverService(exe, port, args, environment);243            } catch (IOException e) {244                throw new WebDriverException(e);245            }246        }247        private File findDesktopDriverExecutable() {248            return findExecutable(DESKTOP_DRIVER_SERVICE_FILENAME, DESKTOP_DRIVER_EXE_PROPERTY,249                    DESKTOP_DRIVER_DOCS_URL, DESKTOP_DRIVER_DOWNLOAD_URL);250        }251        private File findSilverlightDriverExecutable() {252            return findExecutable(SILVERLIGHT_DRIVER_SERVICE_FILENAME, SILVERLIGHT_DRIVER_EXE_PROPERTY,253                    SILVERLIGHT_DRIVER_DOCS_URL, SILVERLIGHT_DRIVER_DOWNLOAD_URL);254        }255        private File findStoreAppsDriverExecutable() {256            return findExecutable(STORE_APPS_DRIVER_SERVICE_FILENAME, STORE_APPS_DRIVER_EXE_PROPERTY,...Source:TestClass.java  
...8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.interactions.Actions;10import org.openqa.selenium.winium.DesktopOptions;11import org.openqa.selenium.winium.WiniumDriver;12import org.openqa.selenium.winium.WiniumDriverService;13import org.testng.annotations.AfterClass;14import org.testng.annotations.BeforeClass;15import org.testng.annotations.Test;1617public class TestClass {1819	public static WebDriver driver;20	public static WiniumDriverService service;21	public static DesktopOptions options;22	public static void main(String[] args) throws Exception{23		driverInvoke();24		firstTest();25		stopServices();26	}272829	@BeforeClass30	public static void driverInvoke() throws Exception{31		options = new DesktopOptions();32		options.setApplicationPath("C:\\Program Files (x86)\\SAP\\FrontEnd\\SapGui\\saplogon.exe");3334		File driverPath = new File(System.getProperty("user.dir")+"\\Winium.Desktop.Driver.exe");35		WiniumDriverService service = new WiniumDriverService.Builder().usingDriverExecutable(driverPath).usingPort(9999).withVerbose(true).withSilent(false).buildDesktopService();36		try {				37			service.start();38		} catch (IOException e) {39			System.out.println("Exception while starting WINIUM service");40			e.printStackTrace();41		}42		driver = new WiniumDriver(service,options);43		Thread.sleep(5000);44	}4546	@Test47	public static void firstTest() throws Exception{48		System.out.println("First Test");49		System.setProperty("webdriver.gecko.driver","C:\\SAP Automation\\SAPTransport\\libs\\geckodriver.exe");
...Source:FileUpload.java  
...4import org.openqa.selenium.By;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.winium.DesktopOptions;7import org.openqa.selenium.winium.WiniumDriver;8import org.openqa.selenium.winium.WiniumDriverService;9import com.qaninjas.driverfactory.DriverFactory;10public class FileUpload {11	WiniumDriver d;12	private WiniumDriverService service;13	14	protected WebDriver driver = DriverFactory.getInstance().getDriver();15	private static  Logger logger = Logger.getLogger(FileUpload.class);16	17	public void fileUpload(By locator, String uploafFilePath) {18		try {19			startService();20			driver.findElement(locator).click();21			d.findElement(By.name("path")).sendKeys(uploafFilePath);22			d.findElement(By.name("Open")).click();23			logger.info("File uploaded successfully");24		} catch (Exception e) {25			logger.info(e.getMessage());26		}27		stopService();28	}29	private void stopService() {30		try {31			if(null == service) {32				service = new WiniumDriverService.Builder()33						.usingDriverExecutable(new File(System.getProperty("user.dir")34								+ "/src/main/resource/lib/Winium.Desktop.Driver.exe"))35						.usingPort(9999).withVerbose(true).buildDesktopService();36				service.start();37				DesktopOptions options = new DesktopOptions();38				options.setApplicationPath("C:\\Windows\\System32\\notepad.exe");39				d = new WiniumDriver(service, options);40			}41		} catch (Exception e) {42			logger.info(e.getMessage());43		}44	}45	private void startService() {46		try {...WiniumDriverService
Using AI Code Generation
1import java.io.File;2import java.io.IOException;3import java.net.URL;4import java.util.concurrent.TimeUnit;5import org.openqa.selenium.winium.DesktopOptions;6import org.openqa.selenium.winium.WiniumDriver;7import org.openqa.selenium.winium.WiniumDriverService;8public class WiniumTest {9public static void main(String[] args) throws IOException {10    DesktopOptions options = new DesktopOptions();11    options.setApplicationPath("C:\\Program Files (x86)\\Notepad++\\notepad++.exe");12    WiniumDriverService service = new WiniumDriverService.Builder()13            .usingDriverExecutable(new File("C:\\Program Files (x86)\\Winium\\Winium.Desktop.Driver.exe"))14            .usingPort(9999).withVerbose(true).withSilent(false).buildDesktopService();15    service.start();16    WiniumDriver driver = new WiniumDriver(service, options);17    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);18    driver.findElementByClassName("Edit").sendKeys("Hello World!");19    driver.findElementByName("File").click();20    driver.findElementByName("Exit").click();21    driver.quit();22    service.stop();23}24}25[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ winium-java-client ---26[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ winium-java-client ---27[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ winium-java-client ---WiniumDriverService
Using AI Code Generation
1package com.packt.webdriver.chapter3;2import java.io.File;3import java.io.IOException;4import java.net.MalformedURLException;5import java.net.URL;6import java.util.concurrent.TimeUnit;7import org.openqa.selenium.By;8import org.openqa.selenium.winium.DesktopOptions;9import org.openqa.selenium.winium.WiniumDriver;10import org.openqa.selenium.winium.WiniumDriverService;11public class WiniumDriverServiceDemo {12	public static void main(String ...args) throws IOException, InterruptedException{13		DesktopOptions options = new DesktopOptions();14		options.setApplicationPath("C:\\Windows\\System32\\calc.exe");15		WiniumDriverService service = new WiniumDriverService.Builder()16				.usingDriverExecutable(new File("C:\\Winium\\Winium.Desktop.Driver.exe"))17				.usingPort(9999)18				.withVerbose(true)19				.withSilent(false)20				.buildDesktopService();21		service.start();22		WiniumDriver driver = new WiniumDriver(service, options);23		driver.findElement(By.name("Seven")).click();24		driver.findElement(By.name("Plus")).click();25		driver.findElement(By.name("Eight")).click();26		driver.findElement(By.name("Equals")).click();27		driver.findElement(By.name("Close")).click();28		service.stop();29	}30}31package com.packt.webdriver.chapter3;32import java.io.File;33import java.io.IOException;34import java.net.MalformedURLException;35import java.net.URL;36import java.util.concurrent.TimeUnit;37import org.openqa.selenium.By;38import org.openqa.selenium.winium.DesktopOptions;39import org.openqa.selenium.winium.WiniumDriver;40import org.openqa.selenium.winium.WiniumDriverService;41public class WiniumDriverServiceDemo {42	public static void main(String ...args) throws IOException, InterruptedException{43		DesktopOptions options = new DesktopOptions();44		options.setApplicationPath("C:\\Windows\\System32\\calc.exe");45		WiniumDriverService service = new WiniumDriverService.Builder()46				.usingDriverExecutable(new File("C:\\WinWiniumDriverService
Using AI Code Generation
1package winiumdemo;2import java.io.File;3import java.io.IOException;4import org.openqa.selenium.winium.DesktopOptions;5import org.openqa.selenium.winium.WiniumDriver;6import org.openqa.selenium.winium.WiniumDriverService;7public class WiniumDemo {8    public static void main(String[] args) throws InterruptedException, IOException {9        DesktopOptions options = new DesktopOptions();10        options.setApplicationPath("C:\\Windows\\System32\\calc.exe");11        WiniumDriverService service = new WiniumDriverService.Builder().usingDriverExecutable(new File("C:\\winium\\Winium.Desktop.Driver.exe")).usingPort(9999).withVerbose(true).withSilent(false).buildDesktopService();12        service.start();13        WiniumDriver driver = new WiniumDriver(service, options);14        WebElement one = driver.findElementByName("One");15        one.click();16        WebElement plus = driver.findElementByName("Plus");17        plus.click();18        WebElement two = driver.findElementByName("Two");19        two.click();20        WebElement equals = driver.findElementByName("Equals");21        equals.click();22        WebElement result = driver.findElementByName("Display is 3");23        String text = result.getText();24        System.out.println("Text on the calculator is: " + text);25        Thread.sleep(5000);26        driver.close();27        driver.quit();Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
