Transitioning to Protractor as a UI automation framework after extensive use of Selenium in Java, Ruby, and Groovy has presented me with new challenges. As a newcomer to Javascript, I'm uncertain about the transferability of my previous techniques.
In Ruby and Groovy, I found a "multiwait" library particularly helpful for waiting on a set of mutually exclusive events. This library involved a map where keys described events and values were executable code chunks. The function would iterate through each event, returning the key when an event returned true. If no event returned true within a specified timeout, an Exception summarizing the events and wait time was thrown. Additionally, there was a special "nothing" event for scenarios where an action might not trigger any response if data was good. In such cases, the function would return the key for the "nothing" event instead of throwing an Exception.
Now, I am attempting to replicate this functionality in Javascript, facing the challenge of adapting to the language's syntax and features.
I understand that functions can be stored as variables in Javascript. Should I consider using functions as event values in a Hash Table for this purpose? Will it introduce scope issues, or should everything work smoothly as long as each function can access the necessary variables?
If someone could provide me with a simple example, it would greatly assist me. Let's assume I have a page object with a method getColor
which always returns the String red
. I aim to create a method in another file that accepts a Hash Table of events like:
var WaitForEvent = require('../../waitForEvent');
var wait = new WaitForEvent();
function getColor() {
return 'red';
}
var outcomes = {};
outcomes['Data accepted'] = function () { getColor() == 'green' };
outcomes['Data rejected'] = function () { getColor() == 'red' };
var result = wait.waitFor(outcomes);
expect(result).toBe('Data accepted');
This outlines how I plan to define parameters for the method. Would functions like these be suitable for this scenario? Could a waitForEvent function effectively iterate through these functions, determining which one returns true
first, and then return the corresponding event key? Or do I need to approach this differently?
If this approach is viable, how should the method handle looping through nameless functions within the Hash Table values? What is the correct way to execute and evaluate the return value of such functions?
While familiar with Selenium's ExpectedConditions
feature, chaining conditions with the OR
operator, I seek a more flexible alternative.
Thank you!