This is a migrated thread and some comments may be shown as answers.

InvokeScript - how to reference objects?

3 Answers 132 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
tsjax
Top achievements
Rank 1
tsjax asked on 26 Sep 2016, 04:20 PM

I am very new to Telerik, so forgive my nearly total lack of knowledge in this area. I am coming from Selenium and having a difficult time trying to find the equivalent ways to do things in Telerik.

One example is simply invoking JavaScript by reference elements that are available in c#. For example,

C# fake code of what I'm trying to accomplish:

var inputbox = myclass.FindElement.ByXPath<HtmlInputText>("//*[@id='someid']//input[@type='someotherid']");
mybrowser.Actions.InvokeScript( inputbox.HowToReferToThisInJS?? +".some_method_like_click_or_whatever_in_js()" );

Sorry if this isn't clear and I'm missing something super obvious. This was relatively straight forward in Selenium (at least I was used to it) and I'm sure there is an easy way to do this using Telerik, I'm just having a hard time bridging the gap.

Something similar that works in Java+Selenium (the arguments[0-1] are magically brought in from the parameters in the method call - this is essentially what I'm trying to accomplish):

WebElement dropdown = driver().findElement(By.xpath(xpath)); // A dropdown box
int index = 5;
((JavascriptExecutor)driver()).executeScript("var select = arguments[0]; select.options[arguments[1]].selected = true;", dropdown, index);

 

Thanks.

3 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 29 Sep 2016, 06:51 AM
Hi,

Can you please give a simple example for what you are trying to achieve? If you want to attach event handler to some control's event you can do that in the code-behind in a similar manner:

In the markup:
<telerik:RadTextBox runat="server" ID="RadTextBox1"></telerik:RadTextBox>

In the code-behind:
protected void Page_Load(object sender, EventArgs e)
{
    RadTextBox1.ClientEvents.OnFocus = "focusHandler";
}

For client-side logic you should take a look at our documentation on how to get reference to control:

Best Regards,
Konstantin Dikov
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
tsjax
Top achievements
Rank 1
answered on 30 Sep 2016, 12:53 PM
I appreciate your answer. This is very useful information and will come in handy a little later.

Right now, I am just trying to figure out how to access objects, such as web elements (e.g. a dropbox) via invoked JavaScript. For example, in c# I have identified an element as such:

var dbox = this.FindElement.ByClass<HtmlSelect>( …. );

How do I interact with dbox in an InvokedJavascript instruction? In other words, how do I pass dbox to JavaScript and access its attributes and methods in JavaScript?

Here is exactly how I would do this in Java using Selenium:

WebElement dbox = driver.findElement(By.xpath(xpath)); // A dropdown box
int index = 5; // An integer
((JavascriptExecutor)driver).executeScript("var select = arguments[0];
  select.options[arguments[1]].selected = true;",
  dbox, index);


In this example, I am able to pass dbox and the integer as arguments into JavaScript and interact with them natively.

Thanks for your help.
0
Konstantin Dikov
Telerik team
answered on 30 Sep 2016, 02:20 PM
Hello,

I would suggest that you take a look at our Test Studio product, which is specialized in Automating AJAX, Silverlight, MVC:
Here is the alternative for InvokeScript in Test Studio:
Here is an example for getting reference to particular element with jQuery for example, where you are passing the ID of the control as selector:
script = String.Format(@"$telerik.$(""#{0}"").css(""{1}"")", controlID, "height");
return ConvertToInt(Manager.Current.ActiveBrowser.Actions.InvokeScript(script));

For getting reference to the client-side object of the controls from our suite you will have to use the CilentID and pass it to $find(clientIDofTheControl) method.

You can also execute scripts with the following approaches:
Hope this helps.


Regards,
Konstantin Dikov
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
Ajax
Asked by
tsjax
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
tsjax
Top achievements
Rank 1
Share this question
or