-2

I'm more and more struggeling with the following problem. Depending on the testdata a different button or link is present which have to be clicked on to continue to the next teststep. When I'm waiting for button A to be clickable while button B is present the test will fail because of a Time out and visa versa.

What strategy can be used to solve this problem?

I found the following Question: Selenium Wait for anyone of Element to visible

Here the problem is solved with

Expectedconditions.or

This is available in Java but not in C#.

I tried this construction but this is not allowed in C#:

 wait.Until(d =>
        ExpectedConditions.ElementToBeClickable(element1) || ExpectedConditions.ElementToBeClickable(element2));

It gives the following error message: "Operator "||"cannot be applied to operands of type 'Func' and 'Func'.

Frank
  • 831
  • 1
  • 11
  • 23
  • 3
    It would be awesome if you could provide a [mcve]. – mjwills Jun 04 '18 at 13:38
  • Coding is not the problem here, it's more a general problem. – Frank Jun 04 '18 at 13:40
  • Have you tried creating a custom expectedcondition. You can put anything inside it. - https://stackoverflow.com/questions/21339339/how-to-add-custom-expectedconditions-for-selenium – Grasshopper Jun 04 '18 at 14:52
  • I'm struggeling with it right now. To wait till one of the two is clickable no problem but how to find out wich one it is and then click on that one? – Frank Jun 04 '18 at 14:55
  • For the people downvoting my question, can you explain me what code example can add value to the question. I think the question is clear without code. – Frank Jun 04 '18 at 15:29
  • @Frank Check the clickabikity fr each element in a if condition. If true then return that element – Grasshopper Jun 04 '18 at 15:56
  • So first wait till one of the two is clickable with the help of explicit wait and after that check wich of the two is clickable. I will check this solution tomorrow. Is this possible with a non existing Webelement? – Frank Jun 04 '18 at 16:31
  • Nope put the if conditions inside the custom wait... – Grasshopper Jun 04 '18 at 16:49

2 Answers2

1

You can use in C# ExpectedConditions.ElementIsVisible(elementLocator) without problem. It is DotNetSeleniumExtras repository in gitHub you can see it here

You can also use it in selenium but is deprecated

Gsanez
  • 61
  • 6
  • But when your waiting till the Element is visible and it is the element wich will not be appear on the page this will end in a time out exception. The problem is that you don't know in advance wich element will appear on the page. – Frank Jun 04 '18 at 14:36
  • 1
    If the location is the same for the different button you try to use xpath. If the position is diferent you have two options, one have N Test to test the differents buttons (I'm assuming that the buttons are not random)or you can catch the expception when they don't find the element and in the catch search for another button the times you need. The test will be a lot longer (10 secs + 10 secs +... + till finding) – Gsanez Jun 04 '18 at 15:21
0

A C# alternative for the Java expression :

Expectedconditions.or

can be:

wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath($"{locatorelement1} | {locatorelement2}")));
Frank
  • 831
  • 1
  • 11
  • 23