Error in module18 | Selenium Forum
R
Rupa Posted on 25/05/2020

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.interactions.Actions;
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 AddDelStockTest {

static WebDriver driver=null;

@Test(priority=1)
public void addStock() 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.manage().window().maximize();
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);
waitForPageToLoad();
WebElement e=driver.findElement(By.id("portfolioid"));
Select s=new Select(e);
s.selectByVisibleText("Ashi_3");
Thread.sleep(2000);
driver.findElement(By.name("+ Add Stock")).click();
Actions act=new Actions(driver);
act.sendKeys(Keys.DOWN).build().perform();
Thread.sleep(2000);

/*driver.findElement(By.id("addstockname")).sendKeys("tat");
driver.findElement(By.id("addstockname")).sendKeys(Keys.DOWN);
driver.findElement(By.id("addstockname")).sendKeys(Keys.DOWN);
driver.findElement(By.id("addstockname")).sendKeys(Keys.DOWN);
System.out.println(driver.findElement(By.id("addstockname")).getText());
JavascriptExecutor js=(JavascriptExecutor)driver;
String script="return document.getElementById('addstockname').value;";

String text=(String) js.executeScript(script);
System.out.println(text);
*/

driver.findElement(By.id("addstockname")).click();
driver.findElement(By.id("addstockname")).sendKeys("Himadri Speciality Chemical Ltd.");
driver.findElement(By.id("stockPurchaseDate")).click();
SelectData("12/08/2019");
driver.findElement(By.id("addstockqty")).sendKeys("100");
driver.findElement(By.id("addstockprice")).sendKeys("500");
WebDriverWait w=new WebDriverWait(driver, 10);
w.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//input[@id='addStockButton']"))));
driver.findElement(By.xpath("//input[@id='addStockButton']")).click();
//input[@id='addStockButton']
waitForPageToLoad();

int rNum=getRowWithCellData("Himadri Speciality Chemical Ltd.");
System.out.println(rNum);

}

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

}


@Test(priority=2,dependsOnMethods={"addStock"})
public void delStock(){

}

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++;
}

}

}


A
Ashish Thakur Replied on 25/05/2020

This is a bug on this site

Its mandatory to select the company from auto box

If you just type it then it will not work


R
Rupa Replied on 25/05/2020

How to write the code for list box element using java script


A
Ashish Thakur Replied on 26/05/2020

Its explained in exercises module number 18


A
Ashish Thakur Replied on 26/05/2020

Its explained in exercises module number 18


A
Ashish Thakur Replied on 26/05/2020

Its explained in exercises module number 18


Related Posts