How to work with if else condition when different element on page are visible at different condition.

In my project i was putting if else for diff condition….but else condition was not working.Then i came out with solution i.e  i put the if condition with try block and else condition in catch block..but now the problem is when the if condition getting false then if block inside try block executing all the statements even the first condition inside try block is false. Please help me….

below is the code@virendra.please look @Test(priority=5)

package S_Package;
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.support.ui.Select;

import org.testng.annotations.Test;
public class WorkCentre_View {

String ticket="";

public WorkCentre_View(String i)

{

ticket=i;

}

WebDriver d = new FirefoxDriver();
@Test(priority=1)

public void serviceDeskLogin() throws InterruptedException

{

d.get("http://182.71.82.92:86/");

d.manage().window().maximize();

Thread.sleep(3000);

Thread.sleep(3000);

d.findElement(By.xpath("//html/body/form/div[2]/div[2]/div[2]/ul/li[2]/input[2]")).sendKeys("scott");

d.findElement(By.xpath("//html/body/form/div[2]/div[2]/div[2]/ul/li[4]/input")).sendKeys("scott@123");

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='frmLogin']/div[2]/div[2]/div[2]/ul/li[5]/input")).click();

Thread.sleep(3000);
}
@Test(priority=2)

public void RoleServiceDesk_NewTicket() throws InterruptedException {

//Click on device received column.

d.findElement(By.xpath("//input[starts-with(@onclick,'AddReceivedTick')]")).click();

Thread.sleep(3000);

//Click on Save

d.findElement(By.xpath(".//*[@id='StatusButton']")).click();

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='dialog-close']")).click();

Thread.sleep(3000);

//Click on ToBeDone

d.findElement(By.xpath(".//*[contains(@href,'javascript://')][starts-with(@onclick,'OpenUpload')]")).click();

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='NewUpload']")).sendKeys("C:\\Users\\SATYA\\Desktop\\20150524_172300-1.jpg");

Thread.sleep(3000);

//Click on Upload All

d.findElement(By.xpath(".//*[@id='btnCancel']")).click();

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='dialog-close']")).click();

Thread.sleep(3000);

//Click on LogOut;

d.findElement(By.xpath(".//*[@id='dvMainHdr']/div[1]/div[2]/a")).click();

Thread.sleep(3000);

}

@Test(priority=3)

public void RoleTester() throws InterruptedException

{

d.get("http://182.71.82.92:86/");

d.manage().window().maximize();

Thread.sleep(5000);

d.findElement(By.xpath("//html/body/form/div[2]/div[2]/div[2]/ul/li[2]/input[2]")).sendKeys("rajesh");

d.findElement(By.xpath("//html/body/form/div[2]/div[2]/div[2]/ul/li[4]/input")).sendKeys("Rajesh@123");

Thread.sleep(5000);

d.findElement(By.xpath(".//*[@id='frmLogin']/div[2]/div[2]/div[2]/ul/li[5]/input")).click();

Thread.sleep(5000);

d.findElement(By.xpath("//a[starts-with(@href,'/TicketInfo/EditTicketInfo?TicketInfoId=')]")).click();

Thread.sleep(5000);

Select sel = new Select(d.findElement(By.id("ProblemCategoryDescriptionID2")));

Thread.sleep(5000);

sel.selectByIndex(3);

Thread.sleep(5000);

Select s = new Select(d.findElement(By.id("FunctionalTestingID2")));

Thread.sleep(5000);

s.selectByIndex(1);

d.findElement(By.xpath(".//*[@id='btnSave']")).click();

Thread.sleep(5000);

d.findElement(By.xpath(".//*[@id='dialog-close']")).click();

Thread.sleep(5000);

}

@Test(priority=4)

public void roleTechSupport() throws InterruptedException

{

d.get("http://182.71.82.92:86/");

d.manage().window().maximize();

Thread.sleep(3000);

d.findElement(By.xpath("//html/body/form/div[2]/div[2]/div[2]/ul/li[2]/input[2]")).sendKeys("david");

d.findElement(By.xpath("//html/body/form/div[2]/div[2]/div[2]/ul/li[4]/input")).sendKeys("david@123");

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='frmLogin']/div[2]/div[2]/div[2]/ul/li[5]/input")).click();

Thread.sleep(5000);

//Assign Technician

Select sl = new Select (d.findElement(By.xpath("//select[starts-with(@onchange,'AddTechnician')]")));

sl.selectByIndex(2);

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='StatusButton']")).click();

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='dialog-close']")).click();

Thread.sleep(3000);

}

@Test(priority=5)

public void roleTechnician1() throws Exception

{

d.get("http://182.71.82.92:86/");

d.manage().window().maximize();

Thread.sleep(3000);

d.findElement(By.xpath("//html/body/form/div[2]/div[2]/div[2]/ul/li[2]/input[2]")).sendKeys("albert");

d.findElement(By.xpath("//html/body/form/div[2]/div[2]/div[2]/ul/li[4]/input")).sendKeys("albert@123");

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='frmLogin']/div[2]/div[2]/div[2]/ul/li[5]/input")).click();

Thread.sleep(3000);

d.findElement(By.xpath("//a[starts-with(@href,'/TicketInfo/EditTicketInfo?TicketInfoId')]")).click();

Thread.sleep(3000);

Select lec = new Select(d.findElement(By.xpath(".//*[@id='RepairLevelID5']")));

lec.selectByIndex(1);

Thread.sleep(3000);

try

{

if(d.findElement(By.id("OriginalQuoteAmount5")).isDisplayed())

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='MainCause5']")).sendKeys("Satya");

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='Diagnosis5']")).sendKeys("Satya");

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='RemedialAction5']")).sendKeys("Satya");

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='btnSave']")).click();

Thread.sleep(5000);

d.findElement(By.xpath(".//*[@id='dialog-close']")).click();

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='btnSelectParts']")).click();

Thread.sleep(3000);

// Check the part you want to select

//d.findElement(By.xpath("//input[starts-with(@onclick,'CheckPartSel')]")).click();

d.findElement(By.xpath("//input[@id='4665']")).click();

Thread.sleep(3000);

//Click on select

d.findElement(By.xpath(".//*[@id='btnPartCheckBox']")).click();

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='dialog-close']")).click();

Thread.sleep(3000);

//Click on Allocate

d.findElement(By.xpath(".//*[@id='grid']/table/tbody/tr/td[1]/u/a")).click();

Thread.sleep(5000);

d.findElement(By.xpath(".//*[@id='dialog-close']")).click();

Thread.sleep(3000);

//Click on FCRC

d.findElement(By.xpath(".//*[@id='grid']/table/tbody/tr/td[1]/a[1]/u")).click();

Thread.sleep(3000);

//Select failure code

d.findElement(By.xpath(".//*[@id='3']")).click();

Thread.sleep(3000);

//Select repair code

Select ct = new Select(d.findElement(By.id("SelectActionCode")));

ct.selectByIndex(2);

Thread.sleep(3000);

//Save FCRC

d.findElement(By.xpath(".//*[@id='btnSavefc']")).click();

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='dialog-close']")).click();

Thread.sleep(3000);

}

catch(Exception E)

{
Select se = new Select(d.findElement(By.id("BillingCodeID5")));

se.selectByIndex(2);

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='MainCause5']")).sendKeys("Satya");

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='Diagnosis5']")).sendKeys("Satya");

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='RemedialAction5']")).sendKeys("Satya");

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='btnSave']")).click();

Thread.sleep(5000);

d.findElement(By.xpath(".//*[@id='dialog-close']")).click();

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='btnSelectParts']")).click();

Thread.sleep(3000);

// Check the part you want to select

//d.findElement(By.xpath("//input[starts-with(@onclick,'CheckPartSel')]")).click();

d.findElement(By.xpath("//input[@id='4665']")).click();

Thread.sleep(3000);

//Click on select

d.findElement(By.xpath(".//*[@id='btnPartCheckBox']")).click();

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='dialog-close']")).click();

Thread.sleep(3000);

//Click on Allocate

d.findElement(By.xpath(".//*[@id='grid']/table/tbody/tr/td[1]/u/a")).click();

Thread.sleep(5000);

d.findElement(By.xpath(".//*[@id='dialog-close']")).click();

Thread.sleep(3000);

//Click on FCRC

d.findElement(By.xpath(".//*[@id='grid']/table/tbody/tr/td[1]/a[1]/u")).click();

Thread.sleep(3000);

//Select failure code

d.findElement(By.xpath(".//*[@id='3']")).click();

Thread.sleep(3000);

//Select repair code

Select ct = new Select(d.findElement(By.id("SelectActionCode")));

ct.selectByIndex(2);

Thread.sleep(3000);

//Save FCRC

d.findElement(By.xpath(".//*[@id='btnSavefc']")).click();

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='dialog-close']")).click();

Thread.sleep(3000);

//Send for customer approval

//d.findElement(By.xpath(".//*[@id='btnCusApproval']"));

Thread.sleep(3000);

}
try

{

if(d.findElement(By.xpath(".//*[@id='btnCusApproval']")).isDisplayed())

{

WebElement web=d.findElement(By.xpath(".//*[@id='btnCusApproval']"));

web.click();

}

}

catch(Exception e1)

{

d.findElement(By.xpath(".//*[@id='grid']/table/tbody/tr/td[1]/u[2]/a")).click();

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='dialog-close']")).click();

Thread.sleep(3000);

}

d.findElement(By.xpath(".//*[@id='dialog-close']")).click();

Thread.sleep(3000);

//Click on Consume

d.findElement(By.xpath(".//*[@id='grid']/table/tbody/tr/td[1]/u[2]/a")).click();

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='dialog-close']")).click();

Thread.sleep(3000);

d.get("http://182.71.82.92:8090/");

Thread.sleep(3000);

d.findElement(By.xpath(".//*[@id='orderId']")).sendKeys(ticket);
}
}
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.