Monday, January 19, 2015

Selenium Javascript


Selenium Script for Chrome Driver
package net.mycom.myapp.selenium;

import static org.junit.Assert.assertEquals;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestPopcornMultipleFunctionsMobile {
  private static WebDriver driver;

  //private static String baseUrl = "http://localhost:8080/";
  //private static String context="mycommyappWebsite/";
  private static String context="home?mobile=true";
  private static String baseUrl = "http://qa.i.opentalk.com/";
  
  private static String login = "seleniumnewuser";
  private static String password = "welcome";
  private static String changePassword = "welcome2";
  private static String testNumber = "2565137992";
  private static String testTextMsg = "hello there...testing with selenium";
  private static String facebookUserName;
  private static String facebookPassword = "";
        private static int countTestUsers = 1;
  private static String loginAs;
  
  @BeforeClass
  public static void setUp() throws Exception {
            for (int i = 1; i <= countTestUsers; i++) {
                System.setProperty("webdriver.chrome.driver", "C:\\Users\\Chrome\\chromedriver.exe");
                driver = new ChromeDriver();
//                driver = new FirefoxDriver();
             driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
             driver.get(baseUrl + context);
//             driver.get(baseUrl);
             System.out.println(baseUrl);
              assertEquals("Phone call | opentalk | Feed", driver.getTitle());
       driver.findElement(By.xpath("//a[@href='loginPage']")).getAttribute("href");
       driver.findElement(By.xpath("//a[@href='loginPage']")).click();
       login(i);
            }
  }
  @AfterClass
  public static void tearDown() throws Exception {
   driver.navigate().to(driver.findElement(By.xpath("//a[@href='logout']")).getAttribute("href"));
   assertEquals("Phone call | open Talk | Login", driver.getTitle());
   driver.quit();
  }
  
  private static void login(int i) {
   loginAs = login + i + "@mycom.net";
   driver.findElement(By.id("emailOrPhone")).clear();
   driver.findElement(By.id("emailOrPhone")).sendKeys(loginAs);
   driver.findElement(By.id("password")).sendKeys(password);
   driver.findElement(By.name("submit")).click();
   assertEquals("Phone call |opentalk | Feed", driver.getTitle());
//   driver.findElement(By.id("menu")).click();
//         sendTextMessage();
        }
  
  //@Test
  public void checkTextHistory() throws Exception {
   driver.findElement(By.linkText("Text History")).click();
//   assertEquals("Text History", driver.getTitle().substring(0,12));
   //driver.findElement(By.xpath("//a[contains(@href,'popupMenu?')]")).click();
   List anchors = driver.findElememycom(By.xpath("//a[contains(@href,'popupMenu?')]"));
   Iterator j = anchors.iterator();
   while(j.hasNext()) {
    WebElement anchor = j.next();
    System.out.println(anchor.getAttribute("href"));
   }
  }
   
  //@Test
  public void sendTextMessage() throws Exception {
   driver.findElement(By.linkText("Call/Text")).click();
   assertEquals("Make Call / Send Text", driver.getTitle().substring(0,21));
   driver.findElement(By.id("sendText_to")).sendKeys(testNumber);
      driver.findElement(By.id("message")).sendKeys(testTextMsg);
   driver.findElement(By.id("sendTextButton")).click();
/*   for (String handle : driver.getWindowHandles()) {
       driver.switchTo().window(handle);
       if (driver.findElement(By.linkText("Send Text")).isDisplayed()) {
        driver.findElement(By.linkText("Send Text")).click();
        break;
       }
   } */
   // Check that we're back to the Text History screen. Don't rely on your variable
   assertEquals("Phone call | open Talk | Text History", driver.getTitle().substring(0,12));
  }
  
  @Test
  public void showHistory() throws Exception {
   driver.findElement(By.id("toggleButton")).click();
         driver.navigate().to(driver.findElement(By.xpath("//a[@href='callHistory']")).getAttribute("href"));
   assertEquals("Phone call | open Talk | Call History", driver.getTitle().substring(0,36));
   driver.findElement(By.id("toggleButton")).click();
         driver.navigate().to(driver.findElement(By.xpath("//a[@href='textHistory']")).getAttribute("href"));
   assertEquals("Phone call | open Talk | Text History", driver.getTitle().substring(0,36));
   
   driver.findElement(By.id("toggleButton")).click();
         driver.navigate().to(driver.findElement(By.xpath("//a[@href='voicemail']")).getAttribute("href"));
   assertEquals("Phone call | open Talk | Voicemail", driver.getTitle().substring(0,33));
   
   driver.findElement(By.id("toggleButton")).click();
         driver.navigate().to(driver.findElement(By.xpath("//a[@href='phoneBookListEntries']")).getAttribute("href"));
   assertEquals("Phone call | open Talk | Phonebook", driver.getTitle().substring(0,33));
   
   driver.findElement(By.id("toggleButton")).click();
         driver.navigate().to(driver.findElement(By.xpath("//a[@href='displaySettingsMobile?appActiveTab=tab7']")).getAttribute("href"));
   assertEquals("Phone call | open Talk | Review Purchase", driver.getTitle().substring(0,39));
         driver.navigate().to(driver.findElement(By.xpath("//a[@href='recentActiveAgemycomearch']")).getAttribute("href"));
      
   driver.findElement(By.id("toggleButton")).click();
         driver.navigate().to(driver.findElement(By.xpath("//a[@href='displaySettingsMobile?hasResponseAction=false']")).getAttribute("href"));
   assertEquals("Phone call | open Talk | Setttings", driver.getTitle().substring(0,33));

        }

  //@Test
  public void showProfile() throws Exception {
   driver.findElement(By.xpath("//a[@href='home']")).click();
   driver.navigate().to(driver.findElement(By.xpath("//a[@href='displaySettings']")).getAttribute("href"));
   assertEquals("Phone call | open Talk | Settings", driver.getTitle());
   driver.findElement(By.xpath("//li[@id='tab2Link']")).click(); //profile
   assertEquals("Phone call | open Talk | Settings", driver.getTitle());
   driver.findElement(By.xpath("//li[@id='tab3Link']")).click(); //phone & voicemail
   assertEquals("Phone call | open Talk | Settings", driver.getTitle());
   driver.findElement(By.xpath("//li[@id='tab4Link']")).click(); //time/date
   assertEquals("Phone call | open Talk | Settings", driver.getTitle());
   driver.findElement(By.xpath("//li[@id='tab5Link']")).click(); //privacy
   assertEquals("Phone call | open Talk | Settings", driver.getTitle());
   driver.findElement(By.xpath("//li[@id='tab6Link']")).click(); //profile picture
   assertEquals("Phone call | open Talk | Settings", driver.getTitle());
   driver.findElement(By.xpath("//li[@id='tab7Link']")).click(); //billing
   assertEquals("Phone call | open Talk | Settings", driver.getTitle());
  }

  @Test
  public void changePassword() throws Exception {
   //change password
   driver.findElement(By.id("toggleButton")).click();
         driver.navigate().to(driver.findElement(By.xpath("//a[@href='displaySettingsMobile?hasResponseAction=false']")).getAttribute("href"));
   driver.navigate().to(driver.findElement(By.xpath("//a[@href='changePassword']")).getAttribute("href"));
   assertEquals("Phone call | open Talk | Settings", driver.getTitle());
   driver.findElement(By.id("changePasswordConfirm_oldPassword")).sendKeys(password); 
   driver.findElement(By.id("changePasswordConfirm_newPassword")).sendKeys(changePassword); 
   driver.findElement(By.id("changePasswordConfirm_confirmNewPassword")).sendKeys(changePassword); 
   driver.findElement(By.id("blueButton")).click();
   assertEquals("Phone call |opentalk | Feed", driver.getTitle());
   
   //logout
   driver.findElement(By.id("toggleButton")).click();
   driver.navigate().to(driver.findElement(By.xpath("//a[@href='logout']")).getAttribute("href"));
   assertEquals("Phone call | open Talk | Login", driver.getTitle());
   
   //logback with new password
         driver.get(baseUrl + context);
   driver.findElement(By.xpath("//a[@href='loginPage']")).getAttribute("href");
   driver.findElement(By.xpath("//a[@href='loginPage']")).click();
   driver.findElement(By.id("emailOrPhone")).clear();
   driver.findElement(By.id("emailOrPhone")).sendKeys(loginAs);
   driver.findElement(By.id("password")).sendKeys(changePassword);
   driver.findElement(By.name("submit")).click();
   
   //revert password
   driver.findElement(By.id("toggleButton")).click();
         driver.navigate().to(driver.findElement(By.xpath("//a[@href='displaySettingsMobile?hasResponseAction=false']")).getAttribute("href"));
   driver.navigate().to(driver.findElement(By.xpath("//a[@href='changePassword']")).getAttribute("href"));
   assertEquals("Phone call | open Talk | Settings", driver.getTitle());
   driver.findElement(By.id("changePasswordConfirm_oldPassword")).sendKeys(changePassword); 
   driver.findElement(By.id("changePasswordConfirm_newPassword")).sendKeys(password); 
   driver.findElement(By.id("changePasswordConfirm_confirmNewPassword")).sendKeys(password);
   driver.findElement(By.id("blueButton")).click();   
   assertEquals("Phone call |opentalk | Feed", driver.getTitle());
//         System.out.println(driver.getTitle());
  }

  @Test
  public void forgotPassword() {
   //logback in
   driver.findElement(By.id("toggleButton")).click();
   driver.navigate().to(driver.findElement(By.xpath("//a[@href='logout']")).getAttribute("href"));
   driver.navigate().to(driver.findElement(By.xpath("//a[@href='passwordResetRequestInit']")).getAttribute("href"));
         System.out.println(loginAs);
   driver.findElement(By.name("emailOrPhone")).clear();
   driver.findElement(By.name("emailOrPhone")).sendKeys(loginAs);
   driver.findElement(By.name("Submit")).click();
   //logback in
         driver.get(baseUrl + context);
   driver.findElement(By.xpath("//a[@href='loginPage']")).getAttribute("href");
   driver.findElement(By.xpath("//a[@href='loginPage']")).click();
   driver.findElement(By.id("emailOrPhone")).clear();
   driver.findElement(By.id("emailOrPhone")).sendKeys(loginAs);
   driver.findElement(By.id("password")).sendKeys(password);
   driver.findElement(By.name("submit")).click();
   assertEquals("Phone call |opentalk | Feed", driver.getTitle());
  }

  //@Test
  public void facebookLogin() {
   facebookUserName = loginAs;
   driver.navigate().to(driver.findElement(By.xpath("//a[@href='logout']")).getAttribute("href"));
   assertEquals("opentalk Login", driver.getTitle());
   driver.findElement(By.xpath("//img[@src='img/facebook_signin3.png']")).click();
   for (String handle : driver.getWindowHandles()) {
       driver.switchTo().window(handle);
       if (driver.findElememycom(By.id("email")).size() != 0) {
        driver.findElement(By.id("email")).clear();
        driver.findElement(By.id("email")).sendKeys(facebookUserName);
     driver.findElement(By.id("pass")).sendKeys(facebookPassword);
     driver.findElement(By.name("login")).click();
        break;
       }
   }
   assertEquals("Phone call | open Talk | opentalk Feed", driver.getTitle());
   
   //logout
   driver.navigate().to(driver.findElement(By.xpath("//a[@href='logout']")).getAttribute("href"));
   assertEquals("Phone call | open Talk | Login", driver.getTitle());
   
   //logback in
   driver.findElement(By.id("login_emailOrPhone")).clear();
   driver.findElement(By.id("login_emailOrPhone")).sendKeys(loginAs);
   driver.findElement(By.id("login_password")).sendKeys(password);
   driver.findElement(By.name("Submit")).click();
   assertEquals("Phone call | open Talk | opentalk Feed", driver.getTitle());
   
  }

  //@Test
  public void showPagination() throws Exception {
//   driver.findElement(By.xpath("//a[@href='home']")).click();
   driver.findElement(By.id("toggleButton")).click();
   driver.findElement(By.cssSelector("a > strong")).click();
   assertEquals("Phone call | open Talk | opentalk Feed", driver.getTitle());
  }


}
Firefox Driver
package net.mycom.myapp.selenium;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import net.mycom.myapp.domain.Account;
import net.mycom.myapp.domain.AccountPending;
import net.mycom.myapp.domain.dao.AccountDao;
import net.mycom.myapp.domain.dao.AccountDaoImpl;
import net.mycom.myapp.util.HibernateUtil;
import net.mycom.myapp.vn.dao.VirtualNumberDao;
import net.mycom.myapp.vn.dao.VirtualNumberDaoImpl;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestmystuffMultipleUser {
 int countTestUsers = 10;
 private static WebDriver driver;

 // private static String baseUrl = "http://localhost:8080/";
 // private static String context = "mycommyappWebsite/";
 private static String context = "";
 //private static String dbUrl = "//mydb/myapp";
 //private static String dbUrl = "//mydb/myapp";
 private static String baseUrl = "http://qa.i.opentalk.com/";
 private static String dbUrl = "//mydbqa.i.mystuff.com/myapp";

 private static String dbLogin = "myapp_web";
 private static String dbPassword = "welcome";
 private static String dbShowSql = "true";
 private static String firstName = "Selenium";
 private static String lastName = "Newuser";
 private static String email = "seleniumNewUser";
 private static String password = "welcome";
 private static String areaCode = "818";
 int terminal = 5555000;
 private static String terminalString = "";

 private static String gender[] = { "Male", "Female" };

 @BeforeClass
 public static void setUp() throws Exception {
  HibernateUtil.setConnectionParameters(dbUrl, dbLogin, dbPassword,dbShowSql);
     System.out.println("Before  FireFox");
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Chrome\\chromedriver.exe");
        driver = new ChromeDriver();
//  driver = new FirefoxDriver();
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
     System.out.println("After  FireFox");
 }

 @AfterClass
 public static void tearDown() throws Exception {
  driver.quit();
 }

 @Test
 public void newUserWithoutVN() throws Exception {
  // loop to check, delete, and create the accoumycom
     System.out.println("Before  Loop");

  for (int i = 0; i < countTestUsers; i++) {

   driver.get(baseUrl + context + "home");
   assertEquals("mystuff Login", driver.getTitle());
   terminal = terminal + i;
   terminalString = areaCode + terminal;
   System.out.println(terminalString);

   Session session = HibernateUtil.getCurremycomession();
   Criteria crit = session.createCriteria(Account.class);
   crit.add(Restrictions.eq("terminatingNumber", terminalString));
   Account ac = (Account) crit.uniqueResult();
   // Delete account if it exists
   if (ac != null) {
    Transaction tran = session.beginTransaction();
    VirtualNumberDao vnDao = new VirtualNumberDaoImpl(null);
    vnDao.deprovision(ac.getAccountId());
    AccountDao accountDao = new AccountDaoImpl();
    accountDao.deleteAccount(ac.getAccountId());
    tran.commit();
   }

   driver.findElement(By.id("signup_firstName")).clear();
   driver.findElement(By.id("signup_firstName")).sendKeys(
     firstName + i);
   driver.findElement(By.id("signup_lastName")).clear();
   driver.findElement(By.id("signup_lastName")).sendKeys(lastName + i);
   driver.findElement(By.id("signup_email")).clear();
   driver.findElement(By.id("signup_email")).sendKeys(
     email + i + "@mycom.net");
   driver.findElement(By.id("signup_email2")).clear();
   driver.findElement(By.id("signup_email2")).sendKeys(
     email + i + "@mycom.net");
   driver.findElement(By.id("signup_password")).clear();
   driver.findElement(By.id("signup_password")).sendKeys(password);
   driver.findElement(By.id("signup_gender")).sendKeys(password);
   Select select = new Select(driver.findElement(By
     .id("signup_gender")));
   // select.deselectAll();
   select.selectByVisibleText(gender[i % 2]);

   driver.findElement(By.xpath("//input[@value='Sign Up']")).click();
   session.close();

   // Reset connection so we can see new inserts
   session = HibernateUtil.getCurremycomession();

   // get from db account pending
   crit = session.createCriteria(AccountPending.class);
   crit.add(Restrictions.eq("email", (email + i + "@mycom.net")));
   crit.addOrder(Order.desc("idAccountPending"));
   crit.setMaxResults(1);
   AccountPending ap = (AccountPending) crit.uniqueResult();
   if (ap == null) {
    fail("could not read account pending");
   }

   // Generate url that the email would send
   String emailCode = ap.getEmailCode();
   driver.navigate().to(
     baseUrl + context + "/emailConfWoVn?id="
       + ap.getIdAccountPending() + "&emailCode="
       + emailCode);

   // terminatingNumber.jsp
   driver.findElement(By.name("areaCode")).sendKeys(areaCode);
   driver.findElement(By.name("teleport")).sendKeys(
     terminalString.substring(3, 6));
   driver.findElement(By.name("terminal")).sendKeys(
     terminalString.substring(6, 10));
   driver.findElement(By.xpath("//input[@value='Continue']")).click();

   // ivrTnVerification.jsp
   driver.findElement(By.xpath("//input[@value='Continue']")).click();

   long accountPendingId = ap.getIdAccountPending();
   session.evict(ap);
   session.close();
   session = HibernateUtil.getCurremycomession();
   ap = (AccountPending) session.get(AccountPending.class,
     accountPendingId);
   Transaction tran = session.beginTransaction();
   ap.setTnCodeVerified(true);
   session.saveOrUpdate(ap);
   tran.commit();

   // ivrTnVerificationConfirm.jsp
   driver.findElement(By.xpath("//input[@value='Continue']")).click();

   // chooseAreaCode.jsp
   driver.findElement(By.xpath("//input[@value='Continue']")).click();

   // chooseVn.jsp
   driver.findElement(By.xpath("//input[@value='Continue']")).click();

   // accountCreated.jsp
   driver.findElement(By.xpath("//input[@value='Go To My Account']"))
     .click();
   driver.navigate().to(
     driver.findElement(By.xpath("//a[@href='logout']"))
       .getAttribute("href"));
   assertEquals("mystuff Login", driver.getTitle());

  }
 }
}

No comments:

Post a Comment