如何用硒填充多次自动建议字段

时间:2017-03-02 05:37:34

标签: selenium testing selenium-webdriver selenium-chromedriver

我目前正在尝试在门户网站上的某个功能上运行多个测试用例,但是autosuggest字段首次运行,但它不能再次运行。我留下证据。

感谢您的帮助。

网址为https://www.bancodeoccidente.com.co/solicitarcredito/#/simuladorLibranza

第一次运作 enter image description here

第二次不起作用,建议列表不会触发 enter image description here

这是我的班级常数:

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 = "https://www.bancodeoccidente.com.co/solicitarcredito/";

    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;
    }

}

这是我的类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;
    }

}

2 个答案:

答案 0 :(得分:0)

尝试以下解决方案:

driver.findElement(By.id(&#34; nombreEmpresaSimulador&#34;))。sendKeys(&#34; G Servicios Integrales Ltda&#34;);

        Robot robot=new Robot();
        robot.keyPress(KeyEvent.VK_TAB);
        robot.keyRelease(KeyEvent.VK_TAB);

        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);

答案 1 :(得分:0)

我使用FluentWait找到了解决方案。

Wait<WebDriver> wait = new FluentWait<>(chrome)
                    .withTimeout(10, TimeUnit.SECONDS)
                    .pollingEvery(200, TimeUnit.MILLISECONDS);

            wait.until(
                    new com.google.common.base.Function<WebDriver, Boolean>() {
                        @Override
                        public Boolean apply(WebDriver driver) {
                            try{
                                businessNameAutosuggestOption = driver.findElement(By.xpath(Constants.LIBRANZA_SIMULATOR_BUSINESS_UTOSUGGEST_OPTION_XPATH));
                            }catch (NoSuchElementException e) {
                                businessNameField.sendKeys(Keys.ENTER);
                                return false;
                            }
                            return true; 
                            }
                        }); 

如果未显示选项列表,则发送一个ENTER键直到显示列表