mercredi 1 mars 2017

How to fill many times a auto suggest field with selenium

I am currently trying to run several test cases on a functionality in a web portal, however a autosuggest field works the first time but it does not work again. I leave evidence.

Thanks for your help.

The URL is http://ift.tt/2lBNczq

The first time it works enter image description here

The second time does not work, the suggested list does not fire enter image description here

This is my class Constants:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Constants {

    public static String URL_TARGET = "http://ift.tt/2mvZ8ai";

    public static String LIBRANZA_SIMULATOR_LINK_XPATH = "//*[@id=\"simulador\"]/div/div/div/ul/li[4]/a/div/span";
    public static String LIBRANZA_SIMULATOR_BUSINESS_NAME_ID = "nombreEmpresaSimulador";
    public static String LIBRANZA_SIMULATOR_BUSINESS_NAME_XPATH = "//*[@id=\"nombreEmpresaSimulador\"]";    
    public static String LIBRANZA_SIMULATOR_BUSINESS_UTOSUGGEST_OPTION_XPATH = "//*[@id=\"simulador\"]/div/div/article/div/div/div/div/form/div[2]/div/div/div/ul/li[1]/a";
    public static String LIBRANZA_SIMULATOR_AMOUNT_XPATH = "//*[@id=\"simulador\"]/div/div/article/div/div/div/div/form/div[3]/div[1]/div/span/input";
    public static String LIBRANZA_CREDIT_TERM_LIST_XPATH = "//*[@id=\"simulador\"]/div/div/article/div/div/div/div/form/div[3]/div[2]/select";
    public static String LIBRANZA_INSURANCE_TOGGLE_XPATH = "//*[@id=\"simulador\"]/div/div/article/div/div/div/div/form/div[6]/div[2]/div/div[1]/div/label";
    public static String LIBRANZA_SIMULATION_RESULT_XPATH = "//*[@id=\"resultado\"]/b";

    public static WebDriver getChromeWebDriver()
    {
        System.setProperty(
                "webdriver.chrome.driver", 
                "files/chromedriver.exe"
                );

        ChromeOptions options = new ChromeOptions();
        options.addArguments("window-size=1280,720");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        WebDriver driver = new ChromeDriver(capabilities);

        return driver;
    }

}

This is my class LibranzaSimulatorTest:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import Util.Constants;

public class LibranzaSimulatorTest {

    private List<String> testCases;
    private WebDriver chrome;
    private WebElement accessLink;
    private WebElement businessNameField;
    private WebElement businessNameAutosuggestOption;
    private WebElement amountField;
    private Select creditTerm;
    private WebElement insuranceToggle;
    private WebElement simulationResult;

    public LibranzaSimulatorTest() 
    {
        testCases = new ArrayList();                
    }

    public void runTest()
    {
        testCases.add("CP001;A LAUMAYER Y CIA EXPORTADORES DE CAFÉ S.A.;$ 57.499.000;96;SI;$ 1.245.412");
        testCases.add("CP002;A TODA HORA S.A. (ATH);$ 156.000.000;96;No;$ 3.113.142");
        initializeComponents();
        for (Iterator iterator = testCases.iterator(); iterator.hasNext();) {
            String string = (String) iterator.next();
            String[] testCase = string.split(";");
            businessNameField.click();
            businessNameField.clear();          
            businessNameField.sendKeys(testCase[1].toLowerCase(), Keys.ENTER);
            WebDriverWait wait  = new WebDriverWait(chrome, 30);
            businessNameAutosuggestOption = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(Constants.LIBRANZA_SIMULATOR_BUSINESS_UTOSUGGEST_OPTION_XPATH)));
            businessNameAutosuggestOption.click();
            amountField.sendKeys(testCase[2]);
            creditTerm.selectByVisibleText(testCase[3]+" Meses");
            insuranceToggle.click();
            String result = simulationResult.getText();
            System.out.println("resultado: "+result);
        }
    }

    private void initializeComponents()
    {
        chrome = Constants.getChromeWebDriver();
        chrome.get(Constants.URL_TARGET);
        chrome.manage().window().maximize();
        WebDriverWait wait  = new WebDriverWait(chrome, 30);
        accessLink = chrome.findElement(By.xpath(Constants.LIBRANZA_SIMULATOR_LINK_XPATH));
        accessLink.click();     
        businessNameField  = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(Constants.LIBRANZA_SIMULATOR_BUSINESS_NAME_XPATH)));
        amountField = chrome.findElement(By.xpath(Constants.LIBRANZA_SIMULATOR_AMOUNT_XPATH));
        creditTerm = new Select(chrome.findElement(By.xpath(Constants.LIBRANZA_CREDIT_TERM_LIST_XPATH)));
        insuranceToggle = chrome.findElement(By.xpath(Constants.LIBRANZA_INSURANCE_TOGGLE_XPATH));
        simulationResult = chrome.findElement(By.xpath(Constants.LIBRANZA_SIMULATION_RESULT_XPATH));
    }

    public List<String> getTestCases() {
        return testCases;
    }

    public void setTestCases(List<String> testCases) {
        this.testCases = testCases;
    }

}

Aucun commentaire:

Enregistrer un commentaire