Calling a function in a JavaScript file with Selenium IDE

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-19 04:19:38

问题


So, I'm running these Selenium IDE tests against a site I'm working on. Everything about the tests themselves is running fine, except I would like to do a bit of clean-up once I'm done. In my MVC3 Razor based site, I have a JavaScript file with a function that gets a JsonResult from a Controller of mine. That Controller handles the database clean-up that Selenium IDE otherwise couldn't handle.

However, I'm having a hard time finding any sort of documentation on how to do this. I know I can do JavaScript{ myJavascriptGoesHere } as one of the Values for a line in the test, but I can't seem to find a way to tell it to go find my clean-up function.

Is it even possible for Selenium IDE to do this sort of thing?

If it comes down to it, I can just make a separate View to handle the clean-up, but I'd really like to avoid that if possible.

Thanks!


回答1:


If you want to execute your own JavaScript function that exists in your test page from Selenium IDE, you need to make sure you access it via the window object. If you look at the reference for storeEval for instance, it says:

Note that, by default, the snippet will run in the context of the "selenium" object itself, so this will refer to the Selenium object. Use window to refer to the window of your application, e.g. window.document.getElementById('foo')

So if you have your own function e.g. myFunc(). You need to refer to it as window.myFunc().

This can be very handy for exercising client-side validation without actually submitting the form, e.g. if you want to test a variety of invalid and valid form field values.

If you use runScript, that should already run in the window's context.




回答2:


This works for me.

    IJavaScriptExecutor js = driver as IJavaScriptExecutor;
    string title = (string)js.ExecuteScript("myJavascriptGoesHere");

Make sure your javascript works first before using it here!




回答3:


Actually to access your page javascript space, you need to get the real window of your page : this.browserbot.getUserWindow()

See this statement to get the jQuery entry point in your page (if it has jQuery of course ^^ )

https://stackoverflow.com/a/54887281/2143734



来源:https://stackoverflow.com/questions/8794528/calling-a-function-in-a-javascript-file-with-selenium-ide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!