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

com.jdiai.tools.JSTalkUtils Maven / Gradle / Ivy

There is a newer version: 1.1.8
Show newest version
package com.jdiai.tools;

import com.jdiai.annotations.UI;
import org.openqa.selenium.By;
import org.openqa.selenium.support.FindBy;

import static com.jdiai.jswraper.JSWrappersUtils.NAME_TO_LOCATOR;
import static java.lang.String.format;
import static org.openqa.selenium.support.How.*;
import static org.openqa.selenium.support.ui.Quotes.escape;

/**
 * Created by Roman Iovlev on 26.09.2019
 * Email: [email protected]; Skype: roman.iovlev
 */
public final class JSTalkUtils {

     public static By uiToBy(UI locator) {
         if (locator == null) return null;
         if (!locator.value().isEmpty()) {
             return NAME_TO_LOCATOR.apply(locator.value());
         }
         if (!locator.id().isEmpty())
             return By.id(locator.id());
         if (!locator.clazz().isEmpty())
             return By.className(locator.clazz());
         if (!locator.xpath().isEmpty())
             return By.xpath(locator.xpath());
         if (!locator.css().isEmpty())
             return By.cssSelector(locator.css());
         if (!locator.text().isEmpty())
             return By.xpath(format(".//*/text()[normalize-space(.) = %s]/parent::*", escape(locator.text())));
         if (!locator.hasText().isEmpty())
             return By.xpath(format(".//*/text()[contains(normalize-space(.), %s)]/parent::*", escape(locator.hasText())));
         if (!locator.tag().isEmpty())
             return By.tagName(locator.tag());
         if (!locator.label().isEmpty())
             return By.cssSelector("[label=" + locator.label() + "]");
         if (!locator.alt().isEmpty())
             return By.cssSelector("[alt=" + locator.alt() + "]");
         if (!locator.hasValue().isEmpty())
             return By.cssSelector("[value=" + locator.hasValue() + "]");
         return null;
     }
     public static By findByToBy(FindBy locator) {
         if (locator == null) return null;
         if (!locator.id().isEmpty())
             return By.id(locator.id());
         if (!locator.className().isEmpty())
             return By.className(locator.className());
         if (!locator.xpath().isEmpty())
             return By.xpath(locator.xpath());
         if (!locator.css().isEmpty())
             return By.cssSelector(locator.css());
         if (!locator.linkText().isEmpty())
             return By.linkText(locator.linkText());
         if (!locator.name().isEmpty())
             return By.name(locator.name());
         if (!locator.partialLinkText().isEmpty())
             return By.partialLinkText(locator.partialLinkText());
         if (!locator.tagName().isEmpty())
             return By.tagName(locator.tagName());
         if (locator.how() != UNSET)
             return getHowLocator(locator);
         return null;
     }
     private static By getHowLocator(FindBy locator) {
         if (locator.how() == ID)
             return By.id(locator.using());
         if (locator.how() == CLASS_NAME)
             return By.className(locator.using());
         if (locator.how() == XPATH)
             return By.xpath(locator.using());
         if (locator.how() == CSS)
             return By.cssSelector(locator.using());
         if (locator.how() == LINK_TEXT)
             return By.linkText(locator.using());
         if (locator.how() == NAME)
             return By.name(locator.using());
         if (locator.how() == PARTIAL_LINK_TEXT)
             return By.partialLinkText(locator.using());
         if (locator.how() == TAG_NAME)
             return By.tagName(locator.using());
         return null;
     }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy