Add Location getting an error as IndexoutofBound Excpetion while handling pagination(Required Code)

20 views
Skip to first unread message

Buddi Chaitanya

unread,
May 17, 2017, 1:30:17 AM5/17/17
to Selenium Users
package addLocation;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import java.awt.AWTException;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.gargoylesoftware.htmlunit.javascript.host.media.webkitMediaStream;
//import com.sun.jna.Structure.FFIType.size_t;

public class AddLocation {
    public static void main(String[] args)throws Exception {
        //System.setProperty("webdriver.gecko.driver", "D:\\Selenium QAPlanet\\Drivers\\geckodriver-v0.15.0-win64\\geckodriver.exe");
        System.setProperty("webdriver.chrome.driver", "D:\\Selenium QAPlanet\\Drivers\\chromedriver_win32\\chromedriver.exe");
        //FirefoxDriver driver=new FirefoxDriver();
        ChromeDriver driver=new ChromeDriver();
        driver.manage().window().maximize();
        WebDriverWait wait=new WebDriverWait(driver, 60);
        Actions Act=new Actions(driver);
        driver.get("http://apps.qaplanet.in/qahrm/login.php");
        //vrifying the home page title
        wait.until(ExpectedConditions.titleIs("OrangeHRM - New Level of HR Management"));
        if(driver.getTitle().equals("OrangeHRM - New Level of HR Management"))
        {
            System.out.println("Home Page is displayed");
        }
        //creating webelements for username,password,login and clear
        WebElement ObjUn=wait.until(ExpectedConditions.presenceOfElementLocated(By.name("txtUserName")));
        WebElement ObjPwd=wait.until(ExpectedConditions.presenceOfElementLocated(By.name("txtPassword")));
        WebElement ObjLogin=wait.until(ExpectedConditions.presenceOfElementLocated(By.name("Submit")));
        WebElement ObjClear=wait.until(ExpectedConditions.presenceOfElementLocated(By.name("clear")));
    //verifying the username
        if(ObjUn.isDisplayed())
    {
        System.out.println("username field is displayed");
    }
    else
    {
        System.out.println("username field is not displayed");
    }
        //verifying the password field
    if(ObjPwd.isDisplayed())
    {
        System.out.println("Password field is displayed");
    }
    else
    {
        System.out.println("Password field is not displayed");
    }
    //verifying the both login and clear fields
    if(ObjLogin.isDisplayed() && ObjClear.isDisplayed())
    {
        System.out.println("Login button and clear buttons are displayed properly");
    }
    else
    {
        System.out.println("Login button and clear buttons are not displayed properly");
    }

    //storing the username and password in objects
    String StrUname="qaplanet1";
    String StrPwd="user1";
    //entering username
    ObjUn.sendKeys(StrUname);
    //entering password
    ObjPwd.sendKeys(StrPwd);
    //click on login
    ObjLogin.click();
    //After logged into HRM, verifying the Home page
    Boolean res=wait.until(ExpectedConditions.titleIs("OrangeHRM"));
    //verifying home page
    if(res)
    {
        System.out.println("home page is displayed properly");
    }
    else
    {
        System.out.println("home page is not displayed");
    }
    //verifying welcome message
    String welcometext=driver.findElement(By.xpath("//ul[@id='option-menu']/li[1]")).getText();
    if(welcometext.equals("Welcome "+StrUname))
    {
        System.out.println("welcome "+StrUname+" is displayed and verified");
    }
    else
    {
        System.out.println("welcome text is not displayed and verified");
    }
    //verifying the change password link and Logout link
    WebElement ObjChangePwd=driver.findElement(By.xpath("//ul[@id='option-menu']/li[2]"));
    WebElement ObjLogout=driver.findElement(By.xpath("//ul[@id='option-menu']/li[3]"));
    if(ObjChangePwd.isDisplayed() && ObjLogout.isDisplayed())
    {
        System.out.println("change password and logout is displayed");
    }
    else
    {
        System.out.println("change password and logout is not displayed");
    }
//focus on Admin
    WebElement objAdmin=driver.findElement(By.linkText("Admin"));
Act.moveToElement(objAdmin).pause(6000).perform();
//webelements for company.job, etc..
WebElement objCompany=driver.findElement(By.xpath("//li[@id='admin']/ul/li/a/span"));
WebElement objJob=driver.findElement(By.xpath("//li[@id='admin']/ul/li[2]/a/span"));
WebElement objQualification=driver.findElement(By.xpath("//li[@id='admin']/ul/li[3]/a/span"));
WebElement objSkills=driver.findElement(By.xpath("//li[@id='admin']/ul/li[4]/a/span"));
    if(objCompany.isDisplayed())
    {
        System.out.println("company link is displayed properly");
    }
    else
    {
        System.out.println("company link is not displayed");
    }
    if(objJob.isDisplayed())
    {
        System.out.println("Job link is displayed properly");
    }
    else
    {
        System.out.println("Job link is not displayed");
    }
    if(objQualification.isDisplayed())
    {
        System.out.println("qualification link is displayed properly");
    }
    else
    {
        System.out.println("qualification link is not displayed");
    }
    if(objSkills.isDisplayed())
    {
        System.out.println("skills link is displayed properly");
    }
    else
    {
        System.out.println("skills link is not displayed");
    }
    //focusing on company info
   
    WebElement ObjCompanyInfo=wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[@id='admin']/ul/li/a/span")));
    Act.moveToElement(ObjCompanyInfo).build().perform();
    //ObjCompanyInfo.click();
    Thread.sleep(3000);
    //Verifying General,Locations, Structure, Properties
    WebElement objCompanyInfo_General=driver.findElement(By.xpath("//li[@id='admin']/ul/li/ul/li/a/span"));
    WebElement objCompanyInfo_Locations=driver.findElement(By.xpath("//li[@id='admin']/ul/li/ul/li[2]/a/span"));
    WebElement objCompanyInfo_CompanyStructure=driver.findElement(By.xpath("//li[@id='admin']/ul/li/ul/li[3]/a/span"));
    WebElement objCompanyInfo_CompanyProperty=driver.findElement(By.xpath("//li[@id='admin']/ul/li/ul/li[4]/a/span"));
    if(objCompanyInfo_General.isDisplayed())
    {
        System.out.println("CompanyInfo_General link is displayed");
    }
    else
    {
        System.out.println("CompanyInfo_General link is not displayed");
    }
    if(objCompanyInfo_Locations.isDisplayed())
    {
        System.out.println("CompanyInfo_Locations link is displayed");
    }
    else
    {
        System.out.println("CompanyInfo_Locations link is not displayed");
    }
    if(objCompanyInfo_CompanyStructure.isDisplayed())
    {
        System.out.println("CompanyInfo_CompanyStructure link is displayed");
    }
    else
    {
        System.out.println("CompanyInfo_CompanyStructure link is not displayed");
    }
    if(objCompanyInfo_CompanyProperty.isDisplayed())
    {
        System.out.println("CompanyInfo_CompanyProperty link is displayed");
    }
    else
    {
        System.out.println("CompanyInfo_CompanyProperty link is not displayed");
    }
   
    //clicking on locations
    objCompanyInfo_Locations.click();
    //wait for frame to be loaded
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("rightMenu")));
    //verifying the Companyinfo_locations text
    WebElement objLocations_info=driver.findElement(By.xpath("//div[@class='maincontent']/form/div/h2"));
    assertEquals(objLocations_info.getText(),"Company Info : Locations");
    System.out.println("Company Info : Locations  text is displayed");

    //verifying location page with serach by
    WebElement objSearchBy=driver.findElement(By.xpath("//div[@class='maincontent']/form/div[2]/label[1]"));
    assertTrue(objSearchBy.isDisplayed());
    System.out.println("Search By is displayed");
WebElement objSearchEditBox=driver.findElement(By.xpath("//div[@class='maincontent']/form/div[2]/input[1]"));
assertTrue(objSearchEditBox.isDisplayed());
System.out.println("Search Edit box is displayed");
WebElement objSearchButton=driver.findElement(By.xpath("//div[@class='maincontent']/form/div[2]/input[2]"));   
assertTrue(objSearchButton.isDisplayed());
System.out.println("Search Button is displayed");
WebElement objResetButton=driver.findElement(By.xpath("//div[@class='maincontent']/form/div[2]/input[3]"));   
assertTrue(objResetButton.isDisplayed());
System.out.println("Reset Button is displayed");
WebElement objAddButton=driver.findElement(By.xpath("//div[@class='actionbuttons']/input[1]"));   
assertTrue(objAddButton.isDisplayed());
System.out.println("Add Button is displayed");
WebElement objDeleteButton=driver.findElement(By.xpath("//div[@class='actionbuttons']/input[2]"));   
assertTrue(objDeleteButton.isDisplayed());
System.out.println("Add Button is displayed");
    //clicking on Add button
objAddButton.click();
System.out.println("Add button is cliked");
    wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@class='maincontent']/div/h2"),"Company Info : Locations"));
    System.out.println("Company Info : Locations text is displayed properly");
    //verifying name field
    WebElement objNamField=driver.findElement(By.id("txtLocDescription"));
    assertTrue(objNamField.isDisplayed());
    System.out.println("Name text field is displayed");
    //verifying country dropdown
    WebElement objCountryDropdpwn=driver.findElement(By.id("cmbCountry"));
    assertTrue(objCountryDropdpwn.isDisplayed());
    System.out.println("CountryDropdpwn is displayed");
    //verifying state
    WebElement objState=driver.findElement(By.id("txtState"));
    assertTrue(objState.isDisplayed());
    System.out.println("State field is displayed");
    //verifying city
    WebElement objCity=driver.findElement(By.id("cmbDistrict"));
    assertTrue(objCity.isDisplayed());
    System.out.println("City field is displayed");
    WebElement objAddress=driver.findElement(By.id("txtAddress"));
    assertTrue(objAddress.isDisplayed());
    System.out.println("Address field is displayed");
    //by keeping all fields blank , clicking on save and handling alerts
    WebElement objZipcode=driver.findElement(By.id("txtZIP"));
    WebElement objSaveloc=driver.findElement(By.id("editBtn"));
    objSaveloc.click();
    //wait for alert page
    Alert a=wait.until(ExpectedConditions.alertIsPresent());
    //if(a.getText().equalsIgnoreCase("Please correct the following     %n     - Location Name has to be specified  %n    - Country should be selected! %n   - Address should be specified  %n    - Zip Code should be specified"))
    //{
     System.out.println(a.getText());
    a.accept();
    //}
   
    //enter name field
    String StrLocName="hyderabadmindspace";
    objNamField.clear();
    objNamField.sendKeys(StrLocName);
objSaveloc.click();
    Alert al=wait.until(ExpectedConditions.alertIsPresent());
    System.out.println(al.getText());
    al.accept();
    //selecting country dropdwon
   
    Select s=new Select(objCountryDropdpwn);
    /*List <WebElement> objdropdowncount=s.getOptions();
    //System.out.println(objdropdowncount.size());
    int isize=objdropdowncount.size();
    for(int i=1;i<isize;i++)
    {
        String sValue=objdropdowncount.get(i).getText();
    //System.out.println(sValue);
    }*/
    s.selectByValue("IN");
    if(s.getFirstSelectedOption().getText().equals("India"))
    {
        System.out.println("selected option in drop down is India");
    }
    else
    {
        System.out.println("India is not selected");
    }
    //System.out.println(s.getFirstSelectedOption().getText());
    //System.out.println(s.getAllSelectedOptions());
    objSaveloc.click();
    Alert a2=wait.until(ExpectedConditions.alertIsPresent());
    System.out.println(a2.getText());
    a2.accept();
    //enter the address
    objAddress.clear();
    objAddress.sendKeys("Yellareddy guda, Ameerpet");;
    objSaveloc.click();
    Alert a3=wait.until(ExpectedConditions.alertIsPresent());
    System.out.println(a3.getText());
    a3.accept();
objZipcode.clear();
objZipcode.sendKeys("12345");
    objSaveloc.click();
    driver.switchTo().defaultContent();
    System.out.println("switches to default frame");
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@id='rightMenu']")));
    //wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("rightMenu")));
    //verifying the locations page is displayed or not
    System.out.println("switches to inline frame");
    if(driver.findElement(By.xpath("//div[@class='maincontent']/form/div/h2")).getText().equals("Company Info : Locations"))
    {
    //WebElement objLocationss_info=driver.findElement(By.xpath("//div[@class='maincontent']/form/div/h2"));
    //assertEquals(objLocationss_info.getText(),"Company Info : Locations");
    System.out.println("Company Info : Locations  text is displayed");
    }
    else
    {
        System.out.println("Company Info : Locations  text is not displayed");
    }
    //wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@class='maincontent']/div/h2"),"Company Info : Locations"));
    //System.out.println("Company Info : Locations text is displayed properly");
    //by for loop verifying the loc info
    //Thread.sleep(3000);
   
    /*int rowcount=driver.findElements(By.xpath("//table[@class='data-table']/tbody/tr")).size();
    System.out.println(rowcount);
    int i;
    for(i=1;i<=rowcount;i++)
    {
        //String StrEmpName=driver.findElement(By.xpath("//table[@class='data-table']/tbody/tr["+i+"]/td[3]/a")).getText();
        String LocNamee=driver.findElement(By.xpath("//table[@class='data-table']/tbody/tr["+i+"]/td[3]/a")).getText();
    System.out.println(LocNamee);
        if(LocNamee.equals(StrLocName))
    {
        System.out.println("Locname is "+StrLocName+" and is displayed at: "+i);
    break;
    }*/
    /*else
    {
        System.out.println("Locname is not there");
    }*/
   
    List<WebElement> allrows = driver.findElements(By.xpath("//table[@class='data-table']/tbody/tr"));
    List<WebElement> allpages = driver.findElements(By.xpath("//div[@class='pagingbar']/a"));
        System.out.println("Total pages :" +allpages.size());
        for(int i=0; i<=(allpages.size()); i++)
            {
                for(int row=1; row<=allrows.size(); row++)
                    {
                        System.out.println("Total rows :" +allrows.size());
                        String finalLocname = driver.findElement(By.xpath("//table[@class='data-table']/tbody/tr["+row+"]/td[3]/a")).getText();
                        //System.out.println(finalLocname);
                        System.out.println("Row loop");
                        if(finalLocname.equals("hyderabadmindspace"))
                            {
                                //WebElement editbutton = driver.findElement(By.xpath("//div[@id='table']/table/tbody/tr["+row+"]/td[3]"));
                            System.out.println("Locname is "+StrLocName+" and is displayed at: "+row);
                            //editbutton.click();
                                break;
                            }
                        /*else
                        {
                            System.out.println("Locname is not there");
                        }*/
                        allpages = driver.findElements(By.xpath("//div[@class='pagingbar']/a"));
                    }

                allpages = driver.findElements(By.xpath("//div[@class='pagingbar']/a"));
                //driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
                allpages.get(i).click();
                System.out.println(i);
            }
    //switch to default page
    driver.switchTo().defaultContent();
    WebElement finalLogout=driver.findElement(By.xpath("//ul[@id='option-menu']/li[3]"));
    finalLogout.click();
    //wait for home page
    wait.until(ExpectedConditions.titleIs("OrangeHRM - New Level of HR Management"));
    //Verify OrangeHRM - New Level of HR Management
            if(driver.getTitle().equals("OrangeHRM - New Level of HR Management"))
            {
                System.out.println("Signoff Sucessfull & Home Page displayed");
            }
            else
            {
                System.out.println("Failed to Signoff");
                return;
            }   


    driver.quit();


    }
}



excpetion:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at addLocation.AddLocation.main(AddLocation.java:371)

More Info: In the Pagination link, there are 3 anchor tags, i.e, 2 pages are there , <a>2</a>,  <a>Next</a>, <a>Last</a>,  but next and last are still in anchor tags,
if i click on 2nd page, then due to becuase for , it;s again trying to click on Next(but next link is not highligted, since 2nd page is the last page)


Please help on it

Krishnan Mahadevan

unread,
May 17, 2017, 2:17:38 AM5/17/17
to seleniu...@googlegroups.com

It’s hard to debug your issue by looking at the code.

 

Looking at your stack-trace

 

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4

    at java.util.ArrayList.rangeCheck(Unknown Source)

    at java.util.ArrayList.get(Unknown Source)

    at addLocation.AddLocation.main(AddLocation.java:371)

 

I would suggest that you please setup a break-point in line number 371 in AddLocation.java and see what is the size of the List that you are working with. That should be a good start.

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/9e8a9e4f-0a4c-4b49-96ec-bcb66650bcde%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages