简体   繁体   中英

selenium c# - “lazy” driver.FindElements(By..)

In my effort of trying to find a neat way to validate which buttons are visible to the user based on his Role and Permissions i have encountered a problem. I am using the Page Object Model design pattern with selenium to test a website. A few pages have a different set of buttons which should either be visible or not to the user according to his role.

I keep a Dictionary<Permission,IWebElement> and initialize it in the constructor of the page (class representing a certain page in the site).

All web elements are defined as follows:

private IWebElement btn_openShop => driver.ById("open_shop");

(ById is equivalent to FindsElement(By.Id("open_shop") )

The problem is that if the button shouldn't exist an exception is thrown when adding it to the Dictionary.

Note: moving the initialization of the Dictionary wont help since i test both cases (one in which the user should see the button and one in which he shouldn't).

I changed the Dictionary to be of type <Permission,Lazy<IWebElement>> and added items to it as follows:

dictionary.Add(somePermission,new Lazy<IWebElement>(()=>the button))

Edit: this technique works but it seems that when debugging in visual studio the code crashes. Any thoughts? (no exceptions are thrown during test execution).

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