Search in sources :

Example 1 with LocatorModel

use of com.driver.locator.model.LocatorModel in project selenium_java by sergueik.

the class CsvFileWriter method writeToFile.

@Override
public boolean writeToFile(String fileName, List<LocatorModel> dData) {
    try (CSVWriter writer = new CSVWriter(new FileWriter(ResourceHelper.getResourcePath("locator/") + fileName + ".csv", false), ',')) {
        for (LocatorModel model : dData) {
            String[] str = model.toString().split(":");
            writer.writeNext(str, false);
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
Also used : LocatorModel(com.driver.locator.model.LocatorModel) FileWriter(java.io.FileWriter) CSVWriter(com.opencsv.CSVWriter)

Example 2 with LocatorModel

use of com.driver.locator.model.LocatorModel in project selenium_java by sergueik.

the class ElementLocator method getLocator.

public List<LocatorModel> getLocator(WebDriver aDriver) {
    List<LocatorModel> locatorList = new ArrayList<LocatorModel>();
    locatorList.addAll(getElementsByTag(TAG_LINK, aDriver));
    locatorList.addAll(getElementsByTag(TAG_BUTTON, aDriver));
    locatorList.addAll(getElementsByTag(TAG_DROP_DOWN, aDriver));
    locatorList.addAll(getElementsByTag(TAG_INPUT, aDriver));
    locatorList.addAll(getElementsByTag(TAG_TEXT_AREA, aDriver));
    locatorList.addAll(getElementsByTag(TAG_SPAN, aDriver));
    locatorList.removeIf(new NullRemove());
    return locatorList;
}
Also used : LocatorModel(com.driver.locator.model.LocatorModel) ArrayList(java.util.ArrayList) NullRemove(com.driver.locator.utility.NullRemove)

Example 3 with LocatorModel

use of com.driver.locator.model.LocatorModel in project selenium_java by sergueik.

the class ElementLocator method getXpath.

private LocatorModel getXpath(WebDriver aDriver, Element bElement) {
    REC_COUNT = 1;
    String locator = "";
    Iterator<Attribute> attIterator = bElement.attributes().iterator();
    List<String> locatorList = new ArrayList<>();
    while (attIterator.hasNext()) {
        Attribute attribute = (Attribute) attIterator.next();
        if (IgnoreAttribute.ignoreAttribute.contains(attribute.getKey()) || attribute.getValue().isEmpty())
            continue;
        locator = "//" + bElement.nodeName() + "[@" + attribute.getKey() + "='" + attribute.getValue() + "']";
        locatorList.add(locator);
        if (isUnique(aDriver, By.xpath(locator)))
            break;
        locator = "";
    }
    return locator.length() == 0 ? getXpath(aDriver, bElement.parent(), locatorList.isEmpty() ? new ArrayList<String>(Arrays.asList("//" + bElement.nodeName())) : locatorList) : new LocatorModel("Xpath", locator);
}
Also used : LocatorModel(com.driver.locator.model.LocatorModel) Attribute(org.jsoup.nodes.Attribute) IgnoreAttribute(com.driver.locator.model.IgnoreAttribute) ArrayList(java.util.ArrayList)

Example 4 with LocatorModel

use of com.driver.locator.model.LocatorModel in project selenium_java by sergueik.

the class ElementLocator method getElementsByTag.

private List<LocatorModel> getElementsByTag(String tagName, WebDriver aDriver) {
    Elements eleList = dDocument.getElementsByTag(tagName);
    List<LocatorModel> locator = new ArrayList<>();
    for (int i = 0; i < eleList.size(); i++) {
        locator.add(getUniqueLocator(aDriver, eleList.get(i)));
    }
    return locator;
}
Also used : LocatorModel(com.driver.locator.model.LocatorModel) ArrayList(java.util.ArrayList) Elements(org.jsoup.select.Elements)

Example 5 with LocatorModel

use of com.driver.locator.model.LocatorModel in project selenium_java by sergueik.

the class ElementLocator method getXpath.

private LocatorModel getXpath(WebDriver aDriver, Element bElement, List<String> locatorList) {
    boolean flag = true;
    String locator = "";
    Iterator<Attribute> attIterator = bElement.attributes().iterator();
    List<String> locatorArrayList = new ArrayList<>();
    if (REC_COUNT == 5 || bElement == null || locatorList.isEmpty())
        return null;
    REC_COUNT++;
    while (attIterator.hasNext()) {
        Attribute attribute = (Attribute) attIterator.next();
        if (IgnoreAttribute.ignoreAttribute.contains(attribute.getKey()) || attribute.getValue().isEmpty())
            continue;
        for (String s : locatorList) {
            locator = "//" + bElement.nodeName() + "[@" + attribute.getKey() + "='" + attribute.getValue() + "']" + s;
            locatorArrayList.add(locator);
            if (isUnique(aDriver, By.xpath(locator))) {
                flag = false;
                break;
            }
            locator = "";
        }
        if (!flag)
            break;
    }
    if (locatorArrayList.isEmpty()) {
        for (String s : locatorList) {
            locator = "//" + bElement.nodeName() + s;
            locatorArrayList.add(locator);
            if (isUnique(aDriver, By.xpath(locator)))
                break;
            locator = "";
        }
    }
    return locator.length() == 0 ? getXpath(aDriver, bElement.parent(), locatorArrayList) : new LocatorModel("Xpath", locator);
}
Also used : LocatorModel(com.driver.locator.model.LocatorModel) Attribute(org.jsoup.nodes.Attribute) IgnoreAttribute(com.driver.locator.model.IgnoreAttribute) ArrayList(java.util.ArrayList)

Aggregations

LocatorModel (com.driver.locator.model.LocatorModel)7 ArrayList (java.util.ArrayList)4 IgnoreAttribute (com.driver.locator.model.IgnoreAttribute)2 File (java.io.File)2 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 Attribute (org.jsoup.nodes.Attribute)2 NullRemove (com.driver.locator.utility.NullRemove)1 CSVWriter (com.opencsv.CSVWriter)1 FileOutputStream (java.io.FileOutputStream)1 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)1 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)1 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1 Elements (org.jsoup.select.Elements)1