All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lazerycode.selenium.util.Query Maven / Gradle / Ivy

There is a newer version: 2.0.0-BETA3
Show newest version
package com.lazerycode.selenium.util;

import io.appium.java_client.MobileElement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Select;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Query {

    private RemoteWebDriver driver;
    private String currentBrowserName;
    private boolean isAppium;

    /**
     * Set a static driver object that wil be used for all instances of Query
     *
     * @param driverObject
     */
    public Query initQueryObject(RemoteWebDriver driverObject) {
        driver = driverObject;
        if (null != driver) {
            currentBrowserName = driver.getCapabilities().getBrowserName();
            Object automationName = driver.getCapabilities().getCapability("automationName");
            isAppium = (null != automationName) && automationName.toString().toLowerCase().equals("appium");
        }

        return this;
    }

    private final By defaultLocator;
    private HashMap customLocators = new HashMap<>();

    public Query(By defaultLocator, RemoteWebDriver driver) {
        if (null == defaultLocator) throw new NullPointerException("Query locator cannot be null!");
        this.defaultLocator = defaultLocator;
        initQueryObject(driver);
    }

    public Query(By defaultLocator) {
        if (null == defaultLocator) throw new NullPointerException("Query locator cannot be null!");
        this.defaultLocator = defaultLocator;
    }

    /**
     * Specify a alternate locator for a specific browser.
     * 

* Any actions that use a By object will examine the `browserName` capability of the current driver, * if it matches what you have specified here this locator will be used instead. *

* It is Suggested you pass in a org.openqa.selenium.remote.BrowserType field to ensure accuracy, example: *

* Query query = newQuery(By.id("foo"); * query.addAlternateLocator(BrowserType.GOOGLECHROME, By.id("bar"); *

* This is intentionally a String for future compatibility. * * @param browser String value matching a browsername capability * @param locator A By object used for locating webElements */ public void addAlternateLocator(String browser, By locator) { customLocators.put(browser, locator); } /** * This will return a WebElement object if the supplied locator could find a valid WebElement. * * @return WebElement */ public WebElement findWebElement() { return driver.findElement(locator()); } /** * This will return a MobileElement object if the supplied locator could find a valid MobileElement. * * @return MobileElement */ public MobileElement findMobileElement() { if (isAppium) { return (MobileElement) driver.findElement(locator()); } throw new UnsupportedOperationException("You don't seem to be using Appium!"); } /** * This will return a list of WebElement objects, it may be empty if the supplied locator does not match any elements on screen * * @return List<WebElement>> */ public List findWebElements() { return driver.findElements(locator()); } /** * This will return a list of MobileElement objects, it may be empty if the supplied locator does not match any elements on screen * * @return List<MobileElement>> */ public List findMobileElements() { if (isAppium) { List elementsFound = driver.findElements(locator()); List mobileElementsToReturn = new ArrayList<>(); for (WebElement element : elementsFound) { mobileElementsToReturn.add((MobileElement) element); } return mobileElementsToReturn; } throw new UnsupportedOperationException("You don't seem to be using Appium!"); } /** * This will return a Select object if the supplied locator could find a valid WebElement. * * @return Select */ public Select findSelectElement() { return new Select(findWebElement()); } /** * This will return the locator currently associated with your driver object. * This is useful for passing into ExpectedConditions * * @return By */ public By locator() { checkDriverIsSet(); return customLocators.getOrDefault(currentBrowserName, defaultLocator); } private void checkDriverIsSet() { if (null == driver) { throw new IllegalStateException("Driver object has not been set... You must call 'Query.initQueryObject(driver);'!"); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy