Reputation: 9
I Need your help.Let me describe you my scenario.
I have a login form and 10+ Email,same passwords. I have to write a automation script which took one Email id and password and enter in Login form.If Email id correct than allowed to User go inside in application and shows a "Logout" Link.if Email id is incorrect than show a message "Entered correct Email id and password". Now problem is How i write (IF Condition) to check either allowed to login and not allowed to login because if suppose i wrote if condition for not allowed to login and entered Login id and password is correct than it shows a error message due to failed IF condition.
I tried to write code:
enter code here
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LoginCheckofProcuretiger {
public static WebDriver driver;
public static WebElement web;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
driver= new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
driver.navigate().to("https://eprocure.procuretiger.com/EPROCP");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
//driver.close();
}
@Test
public void test() {
String Mytest[] ={
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
};
int i;
for( i=0;i<Mytest.length;i++ ){
driver.findElement(By.linkText("Login")).click();
driver.findElement(By.id("j_username")).sendKeys(Mytest[i]);
driver.findElement(By.id("j_password")).sendKeys("auction@123");
driver.findElement(By.name("btnlogin")).click();
if(driver.findElement(By.xpath(".//fieldset/div[@class='errorMsg t_space']")).getText() != null)
{
System.out.println("User Not allowed to access. " +Mytest[i]);
}
else
{
driver.findElement(By.linkText("Logout")).click();
driver.findElement(By.linkText("Home")).click();
System.out.println("User allowed to access. " +Mytest[i]);
}
Upvotes: 1
Views: 1132
Reputation: 9
accept
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import jxl.Workbook;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Label;
public class ExportinExcel {
public static WebDriver driver;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//driver=new FirefoxDriver();
// driver.navigate().to("http://www.indianrail.gov.in/tatkal_Scheme.html");
//driver.manage().timeouts().implicitlyWait(100, TimeUnit.MILLISECONDS);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
driver.quit();
}
@Test
public void test() throws IOException, RowsExceededException, WriteException {
//fail("Not yet implemented");
// Given the path where to store Excel.
File FExcel = new File("D:\\software\\Excel\\createExcel.xls");
/* Create a Workbook. */
WritableWorkbook workbookexcel= Workbook.createWorkbook(FExcel);
/* Create a Worksheet. */
workbookexcel.createSheet("Data", 0);
WritableSheet writeablesheet= workbookexcel.getSheet(0);
/* Add Content in row and column and here coumn value increment each time. */
jxl.write.Label Data1 = new jxl.write.Label(0, 0, driver.findElement(By.xpath(".//tr[1]/td[1]/p/b/span")).gettext() );
writeablesheet.addCell(Data1);
jxl.write.Label Data2 = new jxl.write.Label(0, 1, driver.findElement(By.xpath(".//tr[1]/td[2]/p/b/span")).gettext() );
writeablesheet.addCell(Data2);
jxl.write.Label Data3 = new jxl.write.Label(0, 2, driver.findElement(By.xpath(".//tr[1]/td[3]/p/b/span")).gettext() );
writeablesheet.addCell(Data3);
jxl.write.Label Data4 = new jxl.write.Label(1, 0, driver.findElement(By.xpath(".//tr[2]/td[1]/p/b/span")).gettext() );
writeablesheet.addCell(Data4);
jxl.write.Label Data5 = new jxl.write.Label(1, 1, driver.findElement(By.xpath(".//tr[2]/td[2]/p/b/span")).gettext() );
writeablesheet.addCell(Data5);
jxl.write.Label Data6 = new jxl.write.Label(1, 2, driver.findElement(By.xpath(".//tr[2]/td[3]/p/b/span")).gettext() );
writeablesheet.addCell(Data6);
workbookexcel.write();
workbookexcel.close();
}
/html/body/table/tbody/tr/td/table/tbody/tr[1]/td/table/tbody/tr[3]/td/table/tbody/tr/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr[2]/td[2]/table/tbody/tr[1]/td[1]/table/tbody/tr[4]/td/table/tbody/tr[2]/td[1]/p/b/span
.//tr[1]/td[1]/p/b/span
.//tr[1]/td[2]/p/b/span
Upvotes: 0
Reputation: 9
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import jxl.Workbook;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Label;
public class ExportinExcel {
public static WebDriver driver;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//driver=new FirefoxDriver();
// driver.navigate().to("URL");
//driver.manage().timeouts().implicitlyWait(100, TimeUnit.MILLISECONDS);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
driver.quit();
}
@Test
public void test() throws IOException, RowsExceededException, WriteException {
//fail("Not yet implemented");
// Given the path where to store Excel.
File FExcel = new File("D:\\software\\Excel\\createExcel.xls");
/* Create a Workbook. */
WritableWorkbook workbookexcel= Workbook.createWorkbook(FExcel);
/* Create a Worksheet. */
workbookexcel.createSheet("Data", 0);
WritableSheet writeablesheet= workbookexcel.getSheet(0);
/* Add Content in row and column and here coumn value increment each time. */
int i=0,j=0;
for(;j<tablecontent.lenght();j++){
jxl.write.Label Data1 = new jxl.write.Label(i, j, "Enter Path of Table data");
writeablesheet.addCell(Data1);}
/*jxl.write.Label Data2 = new jxl.write.Label(i, j, "Enter Path of Table data");
writeablesheet.addCell(Data2);*/
workbookexcel.write();
workbookexcel.close();
}
}**
Upvotes: 0
Reputation: 9
I used this code and its working
try{
driver.findElement(By.linkText("Login")).click();
driver.findElement(By.id("j_username")).sendKeys(LoginIDs[i]);
driver.findElement(By.id("j_password")).sendKeys(Password[i]);
driver.findElement(By.name("btnlogin")).click();
driver.findElement(By.linkText("Logout")).click();
} catch(Exception e) {
System.out.println("Login with "+ LoginIDs[i] + " : Failed");
} finally {
driver.findElement(By.linkText("Home")).click();
}
Upvotes: 0
Reputation: 3837
It's incredibly slow (and not very elegant) to use (1) try/catch for findElement or (2) count>0 for findElements
It's best to write a check on a case-by-case basis
For example, let's say your login form has either
(1) <input class="can_login"> or (2) <input class="no_login">
To do a faster check than try/catch of count>0, do something like (in Python):
list_of_input_classes = [ elem.get_attribute('class') for elem in driver.find_elements_by_css_selector("form#login > input") ]
if "can_login" in list_of_input_classes:
#you can login
elif "no_login" in list_of_input_classes:
#you can't login
else:
raise Exception( "why were neither of these options found? i.e. unexpected result" )
Upvotes: 0
Reputation: 455
I prefer to use this (see below) because it takes longer when using try{}catch{}
elem = driver.findElements(locator);
if (elem.Count > 0)
{
//Allowed
}
else
{
//Not allowed
}
Upvotes: 0
Reputation: 1525
Instead of if-else you need to use try/catch and test only the presence of Logout link. Haven't tested this code, but i think it should work.
try{
driver.findElement(Logout link locator);
System.out.println("User is allowed access");
.....
}catch(Exception e){
System.out.println("User is NOT allowed access");
.....
}finally{
.....
}
Upvotes: 1