2

すでにインターネットに展開されているJSFページで自動テストを実行しようとしていますが、現在変更できません。

ソースがどのように見えるかを知っています。一部のフィールドにIDまたは名前がないことがわかります。これは、ソースがどのように見えるかを示しています。

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

セレンにIDを与える方法がないため、ブラウザーの要素を手動で検査し、ブラウザーから通知されたIDを使用することにしました。したがって、chromesコンソールからページソースを分析すると、次のように表示されます。

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

ここまでは順調ですね。最初のフィールドでセレンテストを書き始めましたが、テストするとうまくいくようです。

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

問題は、それが検出されない次のフィールドにあります。どうしてか分かりません。私はこれを手に入れます:

スレッド"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)

ご覧のとおり、これはある種のチャットルームであり、最初の入力はニックネームを設定し、2番目の入力はチャットにテストを入力するために使用されます。最初の値を入力すると、ページには2つのフォームがあり、最初のフォームが非表示になり、2番目のフォームが表示されます(ページが更新されます)。

2番目の形式には、サーバーにメッセージを送信する入力フィールドがあり、サーバーはajaxリバーステクノロジーを使用してすべてのクライアントを更新します。AJAXと対話するときにSeleniumに何らかの制限があるかどうかはわかりません。

ここで、残りのテストコードを貼り付けます。機能するのは、最初のフォームをテストするときだけです。

パブリッククラス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 に答える