2

I am trying to perform some automated tests on a JSF page that is already deployed on the internet and i cannot modify currently.

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:

<h:form rendered="#{messagePusher.userName == null}"> 
               <h:messages style="color: red"/>
               Nickname:                       
                <h:inputText id="inputName" value="#{messagePusher.userName}" required="true" requiredMessage="Enter your name!!!"/>
                <br/>
                <h:commandButton action="#{messagePusher.renderChat()}" value="ENTER CHAT"/>               

        </h:form>


        <h:panelGrid id="chatpanel" columns="1" border="0" rendered="#{messagePusher.userName != null}">

            <br/>  
            <h:dataTable value="#{chatMemoryResourse.messages}" var="current">
                    <h:column>
                        <h:outputText value="#{current.message} #{current.sentDateNTime}"
                                      style="color: blue"/>
                    </h:column>
                </h:dataTable>
                <h:form> 
                    <h:messages style="color: red"/>
                    <h:inputText id="input" value="#{messagePusher.messageText}" required="true" requiredMessage="VALUE REQUIRED!"/>
                    <br/>
                    <h:commandButton action="#{messagePusher.writeMessage()}" value="Send message" />
                </h:form>

        </h:panelGrid>

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:

input id="j_idt8:inputName" name="j_idt8:inputName" type="text"

So far so good. I start writing some selenium test for the first of the fields and when i test it seem to work.

selenium.start();
selenium.open(BASE_URL);    
selenium.type("j_idt8:inputName", "Robot"); 
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
selenium.click("j_idt8:_t12");
...

The problem is in the next field it doesn't detect it. I dont know why. I get this:

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)

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).

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.

Here i paste the rest of the test code the only thing that works is when testing the first form:

public class ChatTester {

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();


}     }
4

1 回答 1

1

试试这个代码:

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.waitForCondiditon("selenium.isElementPresent('j_idt17:input')", MAX_WAIT_TIME_IN_MS);
selenium.type("j_idt17:input", "Selenium testing in proggress....");  
selenium.click("j_idt17:_t20");
selenium.waitForCondiditon("selenium.isElementPresent('j_idt17:input')", MAX_WAIT_TIME_IN_MS);
selenium.type("j_idt17:input", "Test over!");  
selenium.click("j_idt17:_t20");
selenium.stop();

您也可以等待 ajax,而不是等待元素出现:

selenium.waitForCondition("selenium.browserbot.getCurrentWindow().jQuery.active == 0;", MAX_WAIT_TIME_IN_MS);

关于 xpath 和 css 的一些材料:w3 xpath 规范xpath 示例CSS 选择器

于 2012-05-01T13:36:01.473 回答