title: How to find a field in a JSF page that is connected with an AJAX push server with Selenium?
body: <p>I am trying to perform some automated tests on a JSF page that is already deployed on the internet and i cannot modify currently.</p>
  
  <p>I know how the source looks like and i see that some of the fields do not have id or name, this is how its source looks like:</p>
  
  <pre><code>&lt;h:form rendered="#{messagePusher.userName == null}"&gt; 
                 &lt;h:messages style="color: red"/&gt;
                 Nickname:                       
                  &lt;h:inputText id="inputName" value="#{messagePusher.userName}" required="true" requiredMessage="Enter your name!!!"/&gt;
                  &lt;br/&gt;
                  &lt;h:commandButton action="#{messagePusher.renderChat()}" value="ENTER CHAT"/&gt;               
  
          &lt;/h:form&gt;
  
  
          &lt;h:panelGrid id="chatpanel" columns="1" border="0" rendered="#{messagePusher.userName != null}"&gt;
  
              &lt;br/&gt;  
              &lt;h:dataTable value="#{chatMemoryResourse.messages}" var="current"&gt;
                      &lt;h:column&gt;
                          &lt;h:outputText value="#{current.message} #{current.sentDateNTime}"
                                        style="color: blue"/&gt;
                      &lt;/h:column&gt;
                  &lt;/h:dataTable&gt;
                  &lt;h:form&gt; 
                      &lt;h:messages style="color: red"/&gt;
                      &lt;h:inputText id="input" value="#{messagePusher.messageText}" required="true" requiredMessage="VALUE REQUIRED!"/&gt;
                      &lt;br/&gt;
                      &lt;h:commandButton action="#{messagePusher.writeMessage()}" value="Send message" /&gt;
                  &lt;/h:form&gt;
  
          &lt;/h:panelGrid&gt;
  </code></pre>
  
  <p>Because there is no way for me to give the ids to selenium i decided to manually inspect the elements in the browser and use the ids that the browser tells me. So when i analyze the page source from my chromes console i see this things like this:</p>
  
  <blockquote>
    <p>input id="j_idt8:inputName" name="j_idt8:inputName" type="text"</p>
  </blockquote>
  
  <p>So far so good. 
  I start writing some selenium test for the first of the fields and when i test it seem to work.</p>
  
  <pre><code>selenium.start();
  selenium.open(BASE_URL);    
  selenium.type("j_idt8:inputName", "Robot"); 
  selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
  selenium.click("j_idt8:_t12");
  ...
  </code></pre>
  
  <p>The problem is in the next field it doesn't detect it. I dont know why.
  I get this:</p>
  
  <blockquote>
    <p>Exception in thread "main"
    com.thoughtworks.selenium.SeleniumException: ERROR: Element
    j_idt17:input not found   at
    com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:112)
      at
    com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:106)
      at
    com.thoughtworks.selenium.DefaultSelenium.type(DefaultSelenium.java:317)
      at chattester.ChatTester.main(ChatTester.java:24)</p>
  </blockquote>
  
  <p>So as you see this is some kind of chat room, the first input sets nickname and the second is used to input test into the chat. The page has 2 forms when the first value is entered the first form hides and the second appears(the page is refreshed). </p>
  
  <p>The second form has an input field that sends a message to a server and the server uses ajax reverse technology to update all clients. I don't know if is there some kind of limitation with Selenium when interacting with AJAX.</p>
  
  <p>Here i paste the rest of the test code the only thing that works is when testing the first form:</p>
  
  <blockquote>
    <p>public class ChatTester {</p>
  
  <pre><code>private static final String MAX_WAIT_TIME_IN_MS = "60000";
  private static final String BASE_URL = "http://somedemoapplication/";    
  
  public static void main(String[] args) {
  
      Selenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", BASE_URL);
      selenium.start();
      selenium.open(BASE_URL);    
      selenium.type("j_idt8:inputName", "Robot");         
      selenium.click("j_idt8:_t12");
  
  
          selenium.type("j_idt17:input", "Selenium testing in proggress....");  
          selenium.click("j_idt17:_t20");
          selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
  
      selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
      selenium.type("j_idt17:input", "Test over!");  
          selenium.click("j_idt17:_t20");
      selenium.stop();
  
  
  }     }
  </code></pre>
  </blockquote>