简体   繁体   中英

Selecting item from dropdown doesn't kick off angularjs function using Selenium

I am trying to write a test with Selenium that selects an item from 2 dropdowns and then clicks a button. The problem I am running into is that the second dropdown is populated by an angularjs call depending on the item selected from the first dropdown. For some reason the angular call never runs and the second list never gets populated and causes an error. Why does the angularjs call not get executed when the value changes?

Sample of dropdown:

<select id="itemList" style="width:495px;" onchange="angular.element($(this)).scope().itemChanged(this);">
   <option value="item1">Item 1</option>
   <option value="item2">Item 2</option>
   <option value="item3">Item 3</option>
</select>

Selenium code:

SelectElement item = new SelectElement(driver.FindElement(By.Id("itemList")));
item.SelectByText("Item 2");

It properly picks the item from the first dropdown, but for some reason the angular call doesn't run to populate the second dropdown; how can I make that call run?

You can't force this trigger by using selenium if it's not triggered as it should be (after click, select or whatever). You could force it by using some javascript. But I would recommend you to try with click() instead of select .

I was able to solve this by manually triggering the onchange() event that made the call to the angular function after the first dropdown was populated.

IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
jse.ExecuteScript("$(arguments[0]).change();", driver.FindElement(By.Id("itemList")));

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