2017-05-19 2 views
0

ロボットフレームワーク - Selenium Webdriver - Java:私の関数にグローバル変数を呼び出すときに、Robot Framework - Selenium Webdriver - Java:グローバル変数を呼び出すときの古い要素参照例外

以下のJavaメソッドを作成し、Robotフレームワークでこのキーワードを呼び出しました。

public String CreateOpportunity() 
{ 
    String OpportunityName = "Optimum Wartung"+RandomNumber(); 
    WaitAndClickElement("id",SalesforceOpportunityPageData.OpportunityTab); 
    ClickOnElement("cssSelector", SalesforceOpportunityPageData.NewButton); 
    SelectDropDownValues ("id",SalesforceOpportunityPageData.SiteCountryField,SalesforceOpportunityPageData.SiteCountryValue); 
EnterValues("id",SalesforceOpportunityPageData.ProjectEndDateField,SalesforceOpportunityPageData.ProjectEndDateValue); 
    ClickOnElement("cssSelector",SalesforceOpportunityPageData.SaveButton); 
    return OpportunityName; 
} 
public void BeginAssess(String opportunityStr){ 
//opportunityString=CreateOpportunity(opportunityStr); 
List<WebElement> opportunities = driver.findElements(By.xpath("//a[contains(@id,'offer-item')]")); 
System.out.println("Entered into Begin Asses function "+ opportunityStr); 

for(WebElement opportunite:opportunities) 
{ 
    WebElement textele = opportunite.findElement(By.cssSelector("div.offer-name.no-contracts")); 
    String textval = textele.getText(); 
    System.out.println("TextValue is: " + textval); 
    if(textval.equalsIgnoreCase(opportunityStr)) 
    { 
     WaitTillElementToBeClickable("cssSelector",CustomerPageData.OpportunityStatus); 
     opportunite.findElement(By.cssSelector("div.opportunity-status")).click(); 
     System.out.println("Its clicked2"); 
    } 
} 
} 
public void WaitTillElementToBeClickable(String locatorType,String locatorVaue) 
{ 
    try{ 
    WebDriverWait wait = new WebDriverWait(driver,200); 

    if(locatorType.equalsIgnoreCase("cssSelector")) 
     wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(locatorVaue))); 

    else if(locatorType.equalsIgnoreCase("xpath")) 
     wait.until(ExpectedConditions.elementToBeClickable(By.xpath(locatorVaue))); 

    else if(locatorType.equalsIgnoreCase("id")) 
     wait.until(ExpectedConditions.elementToBeClickable(By.id(locatorVaue))); 
    } 
    catch(Exception e){ 
     System.out.println("Webdriver Locator Error"+e); 
    } 
} 

以下はRFコードです。変数$ {Oppo}を作成し、これを以下のようにグローバル変数に設定しました。この変数を "Begin Assess"というキーワードに渡します。コードを実行しますが、古い要素の例外が発生します。私は待機状態にしましたが、それでも同じ状況があります。私が間違っているところを助けてください。 注:私はselenium2libraryを使用していません。 Selenium webdriverのみを使用する。

*** Variable *** 
${Oppo} 
*** Test Cases *** 
Create Opportunities in Salesforce Environment 
    Logon To Salesforce 
    ${Oppo}= Create Opportunity 
    Set Global Variable ${Oppo} 
Logon To KCC With Valid Credentials 
    Logon To KCC 
Verify the Salesforce Data is synchronized with KCC tool 
    Update KCC Data 
Complete The Assessment For An Opportunity 
    Search Customer Account Automation 
    Expand Customer Account 
    Begin Assess ${Oppo} 

修正されたコード:

public void BeginAssess(String opportunityStr){ 
//opportunityString=CreateOpportunity(opportunityStr); 
List<WebElement> opportunities = driver.findElements(By.xpath("//a[contains(@id,'offer-item')]")); 
System.out.println("Entered into Begin Asses function "+ opportunityStr); 

for(WebElement opportunite:opportunities) 
{ 
    WebElement textele = opportunite.findElement(By.cssSelector("div.offer-name.no-contracts")); 
    String textval = textele.getText(); 
    System.out.println("TextValue is: " + textval); 
    if(textval.equalsIgnoreCase(opportunityStr)) 
    { 
     WaitTillElementToBeClickable("cssSelector",CustomerPageData.OpportunityStatus); 
     opportunite.findElement(By.cssSelector("div.opportunity-status")).click(); 
     System.out.println("Its clicked"); 
     break; 
    } 
} 
} 
+1

Chrome、Firefox、IEなどの別のブラウザを使用する場合、同じエラーが発生しますか? – Helio

+1

変数を1つのテストケースに設定すると、次のテストケースで何かが記録されます。変数を使用する場所に設定した場所からページが更新されていないことは確実ですか? –

+1

私は、このメソッドで - BeginAssess、アクションをクリックした後ループを壊すと思います。 – Karthikeya

答えて

2

あなたが要素を取得する場合、古い要素例外を取得し、ページの更新後に要素を使用しようとします。リフレッシュの結果がまったく同じページであっても、すべての要素は「古くなってしまいます」。

0

解決策は、ページが更新されるまでsleepになります。

関連する問題