Ekaterina Salazkina
Ekaterina Salazkina

Reputation: 93

Doesn't select the Right option in dropdown

Protractor select the wrong option in dropdown. I have to: select the first dropdown to open another dropdown (in my situation: select Payment Method (1st dropdown) to open the 2nd dropdown - available credit cards): The IMPORTANT moment: if the 1st dropdown has a default value - the 2nd dropdown CAN NOT be visible.

My piece of code had different variation:

let paymentDropdown = element(by.name("payment-method"));
let dynamicBlock = $$(".dynamic").first();
let useThisCardButton = element(by.name("do-add-payment-method»));
let paymentConfigBlock = element(by.id("payment-config-container"));

browser.wait(EC.visibilityOf(paymentDropdown), 5000);
$("#payment-method-0 > option:nth-child(2)").click();
browser.wait(EC.visibilityOf(dynamicBlock), 5000);
useThisCardButton.click();
browser.wait(EC.visibilityOf(paymentConfigBlock), 5000);

So, I’ve tried for the first dropdown:

1. $("#payment-method-0 > option:nth-child(2)").click();
2. element(by.name("payment-method"))
    .all(by.tagName('option'))
    .get(1)
    .click(); 
3. element(by.cssContainingText('option', 'Credit Card')).click();
4. element(by.name("payment-method")).$('[value="cc"]').click();

Html is:

<select name="payment-method" id="payment-method-0" style="display: inline-block;">
<option value="0">Select Payment Method</option>
<option value="cc">Credit Card</option>                    
</select>

And Nothing! I don’t know how does it work, but the 1st dropdown has Default Value, and the 2nd dropdown is VISIBLE! Then I click on «Use this method» button and got Fail! 1 test from 10 tests choose the right 1st value, so test passed. But other 9 trials are failed. Why? How to do it correct and how to be sure that it will select the right option?

Upvotes: 0

Views: 69

Answers (1)

Madhan Raj
Madhan Raj

Reputation: 1442

Try the below one. Here you can select the values without opening the drop down.

var firstDrop = element(by.css('#payment-method-0'));
browser.wait(EC.elementToBeClickable(firstDrop), 5000);
firstDrop.sendKeys('Credit Card');
browser.wait(EC.visibilityOf(dynamicBlock), 5000);

Hope it helps you

Upvotes: 1

Related Questions