1

I am creating a script for saving a record. There is a save button and it has an ID and javascript in it. Below is my script:

[FindsBy(How = How.Id, Using = "ctl00_MainContent_btnSave")]
IWebElement btnSavePCOption1 { get; set; }

public void SaveRequest()
{
   btnSavePCOption1.Click();
}

I called this method and the save button won't work. I replaced it with javascript executor:

IJavaScriptExecutor js = DriverContext.Driver as IJavaScriptExecutor;
js.ExecuteScript("return btnSave.OnClientClick(this, event);");

This line worked. Since I am the only QA in the team and my team has not experienced automation, I consulted my software architect to see if this is valid and he rejected the idea of using a javascript executor because my script is supposed to be mimicking the user's actions. How valid is using a javascript executor on a script if that is the case?

3

Your test is supposed to reproduce the user behavior as close as possible, but you are calling the method attached to the button rather than clicking the button. So it kind of defeats the purpose of the test.

What are the differences ?

  • The Click will ensure that the button is visible and intractable. Calling the method will not.
  • The Click will emit all the events related to a click. Calling the method will not and you'll probably end-up with an unexpected behavior if the page rely on them.

So is there any case where the ExecuteScript is a valid option ?

When the Selenium API doesn't provide a way like:

  • Performing an HTML5 drag and drop or drop of file
  • Getting a Text node
  • Getting the text generated by CSS (pseudo element)
  • Getting the native validation message and status on an input
  • Scrolling to a specific position
  • Waiting for an asynchronous request when the result doesn't change the content of the page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.