Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Webdriver

    Is anyone successfully using WebDriver to test a Smart GWT application?

    Due to the lack of predictable element ids it's difficult to find elements in the page; once you do find them (usually with an unoly xpath expression) they're difficult to manipulate as they're not "real" text boxes/checkboxes etc.

    Correct me if I'm wrong, but the Selenium support in the nightlies doesn't help us much as we're writing our WebDriver tests directly in Java. I suppose what would be ideal would be an implementation of "org.openqa.selenium.By" which understood the "scLocator=" provided by the new Smart GWT Selenium support and an implementation of "org.openqa.selenium.WebElement" which knew how to interact with Smart GWT components.

    Anyone have success with WebDriver and Smart GWT or any other tips?

    Cheers, Robert.

    #2
    WebDriver is not yet supported however you could either use Selenium RC to write your testcases in Java, or use the WebDriver in Selenium Emulation mode.

    As you mention, one could implement a subclass of org.openqa.selenium.By, say ByScLocator, for Smart GWT support. You could try doing this yourself, or sponsor this effort.

    Comment


      #3
      We have a pretty good results doing some UI testing with WebDriver of a SmartGWT application. We were hoping it would get better with the new Selenium support, but the post above by SmartGWT dev seems not hopeful.

      It only works OK with FireFox though.
      We use field.click(); field.click(); to do a double-click which does not work in Chrome. Tests with IE don't even start.

      We didn't go with the XPath way. Like you saw, components in SmartGWT usually don't render as HTML components but as tables with images. So a button to click on, is usually not a real HTML button. Luckily, WebDriver has WebElements which can be anything on the page, and you can also send a click to that element.
      We find most components by looking them up via their CSS class, and usually one of them has the expected text on it.
      Here are some code snippets:

      Code:
      WebElement field, String fieldName, String newValue
      
      //try find a few times, because it doesn't always work
      while (...) {
      WebElement field = driver.findElement(By.name(fieldName));
      }
      
      
      //try a few times, because it doesn't always work in 1 time
      while (...) {
        field.click();
        field.clear();
        field.sendKeys(newValue);
        String value = field.getValue();
      	if (newValue.equals(value)) {
      	return true;
        }
      }
      //always catch these exceptions:
      ElementNotVisibleException
      NoSuchElementException
      StaleElementReferenceException



      To click a button, we find it via its CSS class. You'll need a big understanding of the SmartGWT generated HTML and its CSS, or just use Chrome's developer info on your compiled application.
      e.g. a Button in SmartGWT is rendered in style "buttonTitle"

      Code:
      className = "buttonTitle";
      id = "Submit data";
      
      
      //try a few times, because it doesn't always work in 1 time
      while (...) {
        List<WebElement> buttons = driver.findElements(By.className(className));
        for (WebElement button : buttons) {
      	if (button.getText().equals(id)) {
      		//id should be the text on the button
      		button.click();
      		//wait a bit Thread.sleep
      		...
      	}
        }
      }

      Comment


        #4
        I'm not sure using WebDriver in Selenium mode would be sufficient (Javascript sandbox, and all that.)

        Is there any spec for how ScLocator maps to actual Smart GWT element IDs or do we just have to read the code?

        Levi: we've had success by rendering all our interesting elements with a particular class which is only used for identification, not styling. So instead of getting all the buttons in a page and looping through them to find the one with the correct text, we just say driver.findElement(By.className("idClass-myButton")). This seems to work pretty well, but still gets a bit ugly when you try to do things like look for the content of a particular table cell.

        The reason we're using Smart GWT is so we can be cross-platform, so tests which only work in Firefox aren't very helpful :-(.

        Comment


          #5
          So, I've had some success with creating a WebDriver "By" implementation which works with scLocators, courtesy of the new Selenium support in smartgwt-2.2.jar. More info on the WebDriver Google Group:

          http://groups.google.com/group/webdriver/browse_thread/thread/bbc35e6b7c863d96

          Still having trouble puzzling through the sparsely documented scLocator syntax itself. Any hints greatly appreciated...

          Cheers, Robert.

          Comment


            #6
            Any specific questions we can address? We'd be happy to answer them, then add them to the documentation.

            Comment


              #7
              Is it possible to do a "findElements"? That call seems to be missing in the latest version of AutoTest.js I've seen. If it is possible, what's the SCLocator syntax for returning a list of say, rows in a table (so I can assert the count of rows)?

              What's the syntax to return the image source of a button (so I can tell if a button has the right image file)?

              How do I refer to the element with the title "Baz" inside the window with the title "Foo" (as opposed to the element with the title "Baz" inside the window with the title "Bar", which is on the same page)?

              Cheers, Robert.

              Comment


                #8
                ... also, on the thread on the WebDriver google group you mention finding elements which have specific IDs set, can we set these IDs? How?

                Comment


                  #9
                  Are you in a position to use JavaScript expressions in WebDriver? The locator syntax isn't meant as a programming language, but trivial JavaScript expressions like gridID.getData().getLength() can get you the information you're looking for.

                  Comment


                    #10
                    One your second question, to set an ID, use setID(). If that's not what you meant, please point to the thread you're talking about.

                    Comment


                      #11
                      Originally posted by Isomorphic
                      One your second question, to set an ID, use setID(). If that's not what you meant, please point to the thread you're talking about.
                      Which setID() are you talking about? There's no setID on com.smartgwt.client.widgets.form.fields.FormItem (or any of its ancestors), and it's form elements we're trying to find with Webdriver By.id() expressions.

                      Cheers, Robert.

                      Comment


                        #12
                        Re-reading the documentation included in the smartgwt-2.2 distribution, it mentions "This assumes the ListGrid has an explicit ID". I'm possibly being quite thick, but I honestly can't work out how to set these from code.

                        I'm trying right now to use Selenium IDE to write an integration test. When I click on a button in my page Selenium is producing the following locator:

                        Code:
                        scLocator=//DynamicForm[ID=\"isc_WidgetCanvas_0\"]/element
                        When I try and run this test case (either from Selenium IDE or by copying the generated JUnit test into our Eclipse project) the button is obviously not clicked and the element the test next looks for (which is on the page the button redirects us to) is not found.

                        It would help a lot if I could say "//Button[ID=\"create\"..." but I don't know how.

                        Cheers, Robert.

                        Comment


                          #13
                          ... ok, I'm thick, setID() is on BaseWidget.

                          Comment


                            #14
                            Originally posted by smartgwt.dev
                            WebDriver is not yet supported
                            Any update on this? Plans for a release?

                            Comment


                              #15
                              Something like that ?
                              Code:
                              public By byScId(final String locator) {
                              
                              		  final String id = locator
                              		      .replaceAll("'", "")
                              		      .replaceAll("\"", "");
                              
                              		  return new By() {
                              		    public List<WebElement> findElements(SearchContext ignored) {
                              		      throw new UnsupportedOperationException("findElements");
                              		    }
                              		  
                              		    public WebElement findElement(SearchContext context) {
                              		      if (!(context instanceof JavascriptExecutor)) {
                              		        throw new NoSuchElementException("context must implement JavascriptExecutor");
                              		      }
                              		      
                              		      JavascriptExecutor js = (JavascriptExecutor) context;
                              		      
                              		      return (WebElement) js.executeScript(
                              		          "var o = window[arguments[0]]; " +
                              		          "if (!o) { return null; } " +
                              		          "var scLocator = '//' + o.getClassName() + '[ID=\"' + arguments[1] + '\"]'; " +
                              		          "return window.isc.AutoTest.getElement(scLocator)",
                              		          id, locator);
                              		    }
                              		  };
                              		}

                              Comment

                              Working...
                              X