Robot Framework - Selenium Webdriver - Java: Somebody let me know why i get stale element reference exception when calling a global variable into my function.
I have created the below java method and called this keyword in Robot framework.
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);
}
}
The below is the RF code. I have created a Variable ${Oppo} and set this as Global variable as like below. And passing this variable into the keyword called "Begin Assess". It executes the code, but ending up with stale element exception. I have put the wait condition, but still it has the same situation. Help me where I'm going wrong. Note: I'm not using selenium2library. Using only 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}
Corrected Code:
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;
}
}
}