package module18;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
public class BuyorSellStock {
WebDriver driver;
@Test
public void buyorSellStockTest() throws Exception{
String browser="Chrome";
if (browser.equals("Chrome")) {
driver=new ChromeDriver();
}else if(browser.equals("Morzilla")){
driver=new FirefoxDriver();
}
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.get("https://www.rediff.com/");
// click on money
driver.findElement(By.xpath("//a[@class='moneyicon relative']")).click();
driver.findElement(By.xpath("//*[@id='signin_info']/a[1]")).click();
driver.findElement(By.id("useremail")).sendKeys("ashishthakur1983");//
// driver.findElement(By.id("emailsubmit")).click();
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("userpass"))));
driver.findElement(By.id("userpass")).sendKeys("pass@1234");
driver.findElement(By.id("userpass")).sendKeys(Keys.ENTER);
Thread.sleep(3000);
waitForPageToLoad();
WebElement e=driver.findElement(By.id("portfolioid"));
Select s=new Select(e);
s.selectByVisibleText("Ashi_3");
int rNum=getRowWithCellData("Tata Consultancy");
System.out.println(rNum);
Thread.sleep(2000);
driver.findElement(By.xpath("//table[@id='stock']/tbody/tr["+rNum+"]/td[1]")).click();
driver.findElements(By.xpath("//input[@class='buySell']")).get(rNum-1).click();
}
public void checkTransactionHistoryTest(){
}
public void wait(int time){
try{
Thread.sleep(time*1000);
}catch(Exception e){
e.printStackTrace();
}
}
public void waitForPageToLoad(){
//page load statues
//ajax/jquery statues
JavascriptExecutor js=(JavascriptExecutor)driver;
int i=0;
while (i!=10) {
String state=(String) js.executeScript("return document.readyState;");
System.out.println(state);
if (state.equals("complete")) {
break;
}
else{
wait(2);
}
i++;
}
//check jQuery Statues
i=0;
while (i!=10) {
Long d=(Long) js.executeScript("return jQuery.active");
System.out.println(d);
if (d.longValue()==0) {
break;
}else
wait(2);
i++;
}
}
public void SelectData(String date) throws Exception {
//day,month,year
Date current=new Date();
SimpleDateFormat sd=new SimpleDateFormat("dd/MM/yyyy");
try {
Date selected=sd.parse(date);
String day=new SimpleDateFormat("dd").format(selected);
// String month=new SimpleDateFormat("MM").format(selected); //it will give in numbers
String month=new SimpleDateFormat("MMMM").format(selected); //it will give in alphabets
String year=new SimpleDateFormat("yyyy").format(selected);
System.out.println(day+"---"+month+"---"+year);
String desiredMonthYear=month+" "+year;
Thread.sleep(2000);
while (true) {
String displayedMonthYear=driver.findElement(By.xpath("//div[@class='dpTitleText']")).getText();
Thread.sleep(2000);
if (desiredMonthYear.equals(displayedMonthYear)) {
driver.findElement(By.xpath("//td[text()='"+day+"']")).click();
break;
}else {
if (selected.compareTo(current) > 0) {
driver.findElement(By.xpath("//td[4]//button[1]")).click();
Thread.sleep(2000);
}else if(selected.compareTo(current) < 0){
driver.findElement(By.xpath("//td[2]//button[1]")).click();
}
}
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int getRowWithCellData(String data){
List<WebElement> rows=driver.findElements(By.xpath("//table[@id='stock']/tbody/td"));
for (int rNum = 0; rNum < rows.size(); rNum++) {
WebElement row=rows.get(rNum);
List<WebElement> cells=row.findElements(By.tagName("td"));
for (int cNum = 0; cNum < cells.size(); cNum++) {
WebElement cell=cells.get(cNum);
if (!cell.getText().trim().equals("")&&data.contains(cell.getText())) {
return ++rNum;
}
}
}
return -1;// data not found
}
}
Instructor
Ashish Thakur Replied on 26/05/2020
See the error mesg tr[-1]
Yur row number is coming as -1
So please debug the code