Skip to content

Instantly share code, notes, and snippets.

@toan301
Created August 18, 2016 06:55
Show Gist options
  • Save toan301/9f3221448cab0c516c5007a2a11f3bae to your computer and use it in GitHub Desktop.
Save toan301/9f3221448cab0c516c5007a2a11f3bae to your computer and use it in GitHub Desktop.
bai tap week 5 hoan chinh
package webdriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterClass;
public class Week4 {
WebDriver driver;
@BeforeClass
public void beforeClass() {
// System.setProperty("webdriver.ie.driver", "E:\\Devprograms\\Workspace\\AutomationFramework\\BrowserDriver\\IEDriverServer.exe");
// driver = new InternetExplorerDriver();
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@AfterClass
public void afterClass() {
}
@Test
public void TS01() {
driver.get("http://54.165.24.61:8989/");
//login
type(driver,usname,"automationtesting");
type(driver,pwd,"automationtesting");
click(driver, submit);
click(driver, loan);
click(driver, newButton);
click(driver, applicantNew);
handleIframe(driver, frame1, accNumFrame1, accNameFrame1);
assertFrame(driver, drop1);
click(driver, guarantorNew);
handleIframe(driver, frame2, accNumFrame2, accNameFrame2);
assertFrame(driver, drop2);
click(driver, borrowerNew);
handleIframe(driver, frame3, accNumFrame3, accNameFrame3);
assertFrame(driver, drop3);
click(driver, sponsorNew);
handleIframe(driver, frame4, accNumFrame4, accNameFrame4);
assertFrame(driver, drop4);
click(driver, pledgorNew);
handleIframe(driver, frame5, accNumFrame5, accNameFrame5);
assertFrame(driver, drop5);
click(driver, managerNew);
handleIframe(driver, frame6, accNumFrame6, accNameFrame6);
assertFrame(driver, drop6);
//RETURN TO MAINPAGE
type(driver, loanName, nameSpec);
click(driver, save );
//Assert the entered loan name
WebElement lName = driver.findElement(By.xpath("//input[@id='CereberusDetails_R1_LegalName']"));
Assert.assertEquals(lName.getAttribute("value"), nameSpec);
}
@Test
public void TS02() {
driver.get("http://www.hdfcbank.com/");
Assert.assertTrue(isElementDisplayed(driver,closeAd));
click(driver,closeAd);
String parentWindow = driver.getWindowHandle();
click(driver,agriLink);
switchWindow(driver);
click(driver, accDetail);
switchWindow(driver);
driver.switchTo().frame(frameLogin);
type(driver, cusID, "autotest");
driver.switchTo().defaultContent();
driver.switchTo().frame(frameFooter);
click(driver, pp);
switchWindow(driver);
click(driver, csrLink);
driver.switchTo().window(parentWindow);
}
//TS02
String accDetail = "//p[text()='Account Details']";
String closeAd = "//div[@id='cee_closeBtn']";
String agriLink = "//a[text()='Agri']";
String frameLogin = "login_page";
String frameFooter = "footer";
String cusID = "//input [@class='input_password']";
String pp = "//a[contains(.,'Privacy Policy')]";
String csrLink = "//a[text() = 'CSR']";
//other xpath
String usname = "//input[@id='__uid']";
String pwd = "//input[@id='__pwd']";
String submit = "//button[@type='submit']";
String loan = "//a[contains(text(),'Loans')]";
String newButton = "//span[contains(text(),'New')]";
String applicantNew = "//div[@id = 'field_NewCereberusDetails_R1_Borrower']/a";
String guarantorNew = "//div[@id = 'field_NewCereberusDetails_R1_LoanGuarantor']/a";
String borrowerNew = "//div[@id = 'field_NewCereberusDetails_R1_LoanBorrower']/a";
String sponsorNew = "//div[@id = 'field_NewCereberusDetails_R1_LoanSponsor']/a";
String pledgorNew = "//div[@id = 'field_NewCereberusDetails_R1_LoanPledgor']/a";
String managerNew = "//div[@id = 'field_NewCereberusDetails_R1_LoanManager']/a";
String loanName = "//input[@id='NewCereberusDetails_R1_LegalName']";
String save = "//a[@id='Save']";
//frameApplicant xpath
String frame1 = "ifr_NewCereberusDetails_R1_Borrower";
String drop1 = "NewCereberusDetails_R1_Borrower";
String accNumFrame1 = "//input[@id='NewApplicantForLoan_R1_BorrowerId']";
String accNameFrame1 = "//input[@id='NewApplicantForLoan_R1_BorrowerName']";
String fnFrame1 = "//input[@id='NewApplicantForLoan_R1_FirstName']";
String lnFrame1 = "//input[@id='NewApplicantForLoan_R1_LastName']";
String emailFrame1 = "//input[@id='NewApplicantForLoan_R1_Email']";
String submitFrame = "//input[@type = 'submit']";
//frameGuarantor xpath
String frame2 = "ifr_NewCereberusDetails_R1_LoanGuarantor";
String drop2 = "NewCereberusDetails_R1_LoanGuarantor";
String accNumFrame2 = "//input[@id='NewGuarantorForLoan_R1_BorrowerId']";
String accNameFrame2 = "//input[@id='NewGuarantorForLoan_R1_BorrowerName']";
//frameBorrower xpath
String frame3 = "ifr_NewCereberusDetails_R1_LoanBorrower";
String drop3 = "NewCereberusDetails_R1_LoanBorrower";
String accNumFrame3 = "//input[@id='NewBorrowerForLoan_R1_BorrowerId']";
String accNameFrame3 = "//input[@id='NewBorrowerForLoan_R1_BorrowerName']";
//frameSponsor xpath
String frame4 = "ifr_NewCereberusDetails_R1_LoanSponsor";
String drop4 = "NewCereberusDetails_R1_LoanSponsor";
String accNumFrame4 = "//input[@id='NewSponsorForLoan_R1_BorrowerId']";
String accNameFrame4 = "//input[@id='NewSponsorForLoan_R1_BorrowerName']";
//framePledgor xpath
String frame5 = "ifr_NewCereberusDetails_R1_LoanPledgor";
String drop5 = "NewCereberusDetails_R1_LoanPledgor";
String accNumFrame5 = "//input[@id='NewPledgorForLoan_R1_BorrowerId']";
String accNameFrame5 = "//input[@id='NewPledgorForLoan_R1_BorrowerName']";
//framePledgor xpath
String frame6 = "ifr_NewCereberusDetails_R1_LoanManager";
String drop6 = "NewCereberusDetails_R1_LoanManager";
String accNumFrame6 = "//input[@id='NewManagerForLoan_R1_BorrowerId']";
String accNameFrame6 = "//input[@id='NewManagerForLoan_R1_BorrowerName']";
//random Email/Name
String emailaddr = "toantran" + randomEmail() + "@gmal.com";
String nameSpec = "Tran Sy Toan" + randomEmail();
public String randomEmail(){
Random rd = new Random();
int number = rd.nextInt(9000) + 1;
String numberString = Integer.toString(number);
return numberString;
}
public void type(WebDriver driver, String yourLocator, String yourText) {
WebElement element = driver.findElement(By.xpath(yourLocator));
element.clear();
element.sendKeys(yourText);
}
public void click(WebDriver driver, String yourLocator) {
WebElement element = driver.findElement(By.xpath(yourLocator));
element.click();
}
public boolean isElementSelected(WebDriver driver, String yourLocator){
try{
WebElement element = driver.findElement(By.xpath(yourLocator));
return element.isSelected();
}catch(NoSuchElementException e){
return false;
}
}
public boolean isElementDisplayed(WebDriver driver, String yourLocator){
try{
WebElement element = driver.findElement(By.xpath(yourLocator));
return element.isDisplayed();
}catch(NoSuchElementException e){
return false;
}
}
public void handleIframe(WebDriver driver, String yourFrame, String yourAccNum, String yourName){
driver.switchTo().frame(yourFrame);
type(driver, yourAccNum,"11250");
type(driver, yourName,nameSpec);
try{
if(isElementDisplayed(driver, emailFrame1)){
type(driver, fnFrame1,"Toan");
type(driver, lnFrame1,"Tran");
type(driver, emailFrame1,emailaddr);
}
}catch(NoSuchElementException e){
e.printStackTrace();
}
click(driver,submitFrame);
driver.switchTo().defaultContent();
}
public void assertFrame(WebDriver driver, String yourDropdownList){
Select sl1 = new Select(driver.findElement(By.id(yourDropdownList)));
Assert.assertEquals(sl1.getFirstSelectedOption().getText(), nameSpec);
}
public void switchWindow(WebDriver driver){
Set<String> allWindow = driver.getWindowHandles();
for(String handle : allWindow){
driver.switchTo().window(handle);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment