简体   繁体   中英

Enable and select values of Drop-down based on the value selected in another Drop-down

I have couple of Drop-downs on a page; after selecting a value on the First Drop-down, Second Drop-down will get enable and the values get loaded accordingly.

When running manually, after selecting a value in First Drop-down, the page gets loaded and the Second Drop-down gets enabled and the values are listed according to the value selected in First Drop-down.

However, when do automation using Selenium Webdriver (in Java), only the value of First Drop-down gets selected and the Second Drop-down is never enabled or loaded with the values.

Even tried with WebDriverWait as below, but still no luck.

WebDriverWait wait = new WebDriverWait(wd, 50);
wait.until(ExpectedConditions.elementToBeClickable(By.id("event")));

Noticed that only we move out the page (ALT+TAB)and come again to the page the page is getting loaded and the Second Drop-down is enabled.

Below exception is also thrown,

org.openqa.selenium.TimeoutException: Timed out after 50 seconds waiting for element to be clickable: By.id: event

Please advise.

Coding with Page Object Model:

In POM Class:

//Create Object for Page 1
CreateAsset asset = PageFactory.initElements(wd,CreateAsset.class);


//Read the values of First & Second Drop-down from an excel

String list_event_type = readsheet.getRow(1).getCell(2).getStringCellValue();
String list_event = readsheet.getRow(1).getCell(3).getStringCellValue();

//Calling Drop-down methods in Page Factory POM Class 
asset.selectEventType(list_event_type);

WebDriverWait wait = new WebDriverWait(wd, 50);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("event")));
asset.selectEvent(list_event);

In Page Factory Class:

//select the given value on the First Drop-down

@FindBy(how = How.ID, using = "eventType")
public WebElement   eventType;

public void selectEventType(String eventType)
{
    this.eventType.sendKeys(eventType);
}

//select the given value on Sescond Drop-down

@FindBy(how = How.ID, using = "event")
public WebElement   event;

public void selectEvent(String event)
{
    this.event.sendKeys(event);
}

You are using the same page object to select both values, the problem is that the page object only initializes one (at the very start. This means that the event property never gets initialized with values when the eventType element is changed.

Two options:
1. Re-init the page object after selecting the eventType

//Calling Drop-down methods in Page Factory POM Class 
asset.selectEventType(list_event_type);
asset = PageFactory.initElements(wd,CreateAsset.class);
  1. Change selectEventType to return a new page object, essentially this does the same thing as above

    public CreateAsset selectEventType(String eventType)
    {
    this.eventType.sendKeys(eventType);
    return new CreateAsset(_driver);
    }

Then you would call this in your POM with

asset = asset.selectEventType(list_event_type);

下面的解决方法可以正常工作!

wd.findElement(By.cssSelector("body")).sendKeys(Keys.chord(Keys.ALT, Keys.TAB));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM