InvokeScript - how to reference objects?

4 posts, 0 answers
  1. tsjax
    tsjax avatar
    2 posts
    Member since:
    Sep 2016

    Posted 26 Sep Link to this post

    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.

  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 29 Sep Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. tsjax
    tsjax avatar
    2 posts
    Member since:
    Sep 2016

    Posted 30 Sep in reply to Konstantin Dikov Link to this post

    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.
  5. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 30 Sep Link to this post

    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.
Back to Top