I want to open the mail by clicking on it, after receiving the activation mail.
For that I am using keyword driven framework.
So can anyone please let me know that how can we click on element without using List<>.
Because in my coding structure I am returning the object of Webelement instead of List<> object.
Note: Please suggest the solution without using JavaMail Api or if suggest please let me know according to the keyword driven framework.
Way of code structure where I find the elements in one method and in another method after getting an element perform the operations:
private boolean operateWebDriver(String operation, String Locator,
String value, String objectName) throws Exception {
boolean testCaseStep = false;
try {
System.out.println("Operation execution in progress");
WebElement temp = getElement(Locator, objectName);
System.out.println("Get Element ::"+temp);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//For performing click event on any of the button, radio button, drop-down etc.
if (operation.equalsIgnoreCase("Click")) {
temp.click();
}
/*Trying to click on subject line of "Email"*/
if (operation.equalsIgnoreCase("Click_anElement")) {
Thread.sleep(6000L);
Select select = new Select(temp);
List<WebElement> options= select.getOptions();
for(WebElement option:options){
System.out.println(option.getText());
}
/*try
{
if(temp.getText().equals(value)){
temp.click();
}
}
catch(Exception e){
System.out.println(e.getCause());
}*/
/*if (value != null) {
temp.click();
}*/
}
}
testCaseStep = true;
} catch (Exception e) {
System.out.println("Exception occurred operateWebDriver"
+ e.getMessage());
System.out.println("Taking Screen Shot");
TakesScreenshot ts=(TakesScreenshot)driver;
File source=ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File("./Screenshots/"+screenshotName+".png"));
System.out.println("Screenshot taken");
}
return testCaseStep;
}
public WebElement getElement(String locator, String objectName) throws Exception {
WebElement temp = null;
System.out.println("Locator-->" + locator);
if (locator.equalsIgnoreCase("id")) {
temp = driver.findElement(By.id(objectName));
} else if (locator.equalsIgnoreCase("xpath")) {
temp = driver.findElement(By.xpath(objectName));
System.out.println("xpath temp ----->" + temp);
} else if (locator.equalsIgnoreCase("name")) {
temp = driver.findElement(By.name(objectName));
}else if (locator.equalsIgnoreCase("cssSelector")) {
temp = driver.findElement(By.cssSelector(objectName));
}
return temp;
}
Related
How can I create click method which can be click in the button until the attribute shows? Something like loop until but with timer (if it's not found attribute after 10 seconds, display an error). I have created something like that, but code give me NullPointerException when not found attribute:
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
WebElement button = driver.findElement(By.xpath("xpath"));
String attribute = button.getAttribute("disabled");
button.click();
if(attribute .equals("true"))
return true;
else
return false;
}
});
I got similar problem and my solution was to write a function that tries to click on the object and when it is not present it waits a second and tries once again. After 10 times of trying it returns null.
It works beautifully for me. Goes like this:
WebElement tryfind(WebDriver browser, By id)
{
WebElement ttwel=null;
for (int i=0;i<10;i++)
{
try
{
ttwel=browser.findElement(id);
break;
}
catch (NoSuchElementException ex)
{
sleep(1000);
}
}
return ttwel;
}
void sleep(int mills)
{
try
{
Thread.sleep(mills);
}
catch (InterruptedException ex)
{
}
}
You might add waiting for disabled attribute to this method, something like:
for (int i=0;i<10;i++)
{
try
{
ttwel=browser.findElement(id);
String attribute = button.getAttribute("disabled");
if (attribute==null) sleep(1000);
else break;
}
catch (NoSuchElementException ex)
{
sleep(1000);
}
}
Why don't you add a try catch block like this below to catch the NullPointerException and return false in that case
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
WebElement button = driver.findElement(By.xpath("xpath"));
try {
String attribute = button.getAttribute("disabled");
button.click();
if (attribute.equals("true")) {
return true;
}
} catch (NullPointerException e) {
return false;
}
return false;
}
});
I'm trying to automate query builder, I've managed to create simple queries however unable to create complex or joint queries. The simple query is hardcoded, looking to make it dynamic.
Attaching the image of the query builder for reference. Complex or joint queries to be something like this "StateID = 5 AND STATE = "Bihar"
Below is the code for reference: (simple queries)
public static void selectbyqueries() throws InterruptedException{
String sname=null;
//data queries
//iterate through fields
List<WebElement> rowsList = driver.findElements(By.xpath("//*[contains(#class,'textcolq')]"));
Thread.sleep(1000);
for (WebElement element : rowsList) {
Thread.sleep(500);
try {
sname = element.getText();
if (sname.equalsIgnoreCase("Number")) { //change the data type
Thread.sleep(1000);
element.click();
break;
}
} catch (Exception e) { }
}
driver.findElement(By.xpath("//*[contains(text(),'GET UNIQUE VALUES')]")).click();
//query tools
List<WebElement> query = driver.findElements(By.xpath("//*[contains(#class,'inner-flex ng-star-inserted')]"));
Thread.sleep(1000);
for (WebElement element : query) {
Thread.sleep(500);
try {
sname = element.getText();
//will have to change the tool symbol (stand alone attributes)
if (sname.equalsIgnoreCase("=")) {
highlightclick(element);
Thread.sleep(1000);
element.click();
break;
}
} catch (Exception e) { }
}
//unique values
List<WebElement> unique = driver.findElements(By.xpath("//*[contains(#class,'cursor selected-option ng-star-inserted')]"));
Thread.sleep(1000);
for (WebElement element : unique) {
Thread.sleep(500);
try {
sname = element.getText();
//will
if (sname.equalsIgnoreCase("1")) {
highlightclick(element);
Thread.sleep(1000);
element.click();
break;
}
} catch (Exception e) { }
}
}
Everytime got StaleElementReferenceException exception.
Here is a method, pls help.
private void selectAndClickRow(String elementName, boolean doubleClick) {
try {
String elementXpath = "//tr//td//div[contains(text(),'" + elementName + "')]";
new WebDriverWait(Init.getWebDriver(), Init.getTimeOutInSeconds()).until(ExpectedConditions.visibilityOf(Init.getDriverExtensions().waitUntilElementAppearsInDom(By.xpath(elementXpath))));
WebElement row = table.findElements(By.xpath(elementXpath)).get(0);
row.click();
if (doubleClick) {
row.click();
}
Init.getDriverExtensions().waitUntilElementAppearsInDom(By.xpath("//tr//td[contains(#class,'selected')]//div[contains(text(),'" + elementName + "')]"));
} catch (StaleElementReferenceException e) {
freeze(1);
selectAndClickRow(elementName, doubleClick);
}
waitToLoad();
}
public WebElement waitUntilElementAppearsInDom(By by) {
Wait wait = new WebDriverWait(Init.getWebDriver(), (long)Init.getTimeOutInSeconds());
wait.until(ExpectedConditions.presenceOfElementLocated(by));
return Init.getWebDriver().findElement(by);
}
I already added an element research and waiting for a second. It doesn't help.
I guess, you are trying to double click on a element. You can use actions class as given below instead of clicking twice on a element.
private void selectAndClickRow(String elementName, boolean doubleClick) {
try {
String elementXpath = "//tr//td//div[contains(text(),'" + elementName + "')]";
new WebDriverWait(Init.getWebDriver(), Init.getTimeOutInSeconds()).until(ExpectedConditions.visibilityOf(Init.getDriverExtensions().waitUntilElementAppearsInDom(By.xpath(elementXpath))));
WebElement row = table.findElements(By.xpath(elementXpath)).get(0);
new Actions(driver).doubleClick(row).perform();
} catch (StaleElementReferenceException e) {
//freeze(1);
//selectAndClickRow(elementName, doubleClick);
}
waitToLoad();
}
I've done plenty of research on this question and have tried many different methods, but none of them do what I'd like, or the explanations to implement them into my own code are really vague.
I need to export the test results (TestID, Expected Result, Pass or Fail) into an excel sheet. I am currently using TestNG and Apache POI.
I know how to write to an excel sheet, but I am absolutely lost on how to write whether or not something passed or failed. I am currently using some code that doesn't exactly work - sometimes it will write it, sometimes it won't. I need the most simple, easy way to do this, with a good explanation.
I'll show you my current #BeforeClass, #AfterClass, and two #Test blocks.
#BeforeClass:
#BeforeClass(alwaysRun = true)
public void setupBeforeSuite(ITestContext context) throws IOException {
//create a new work book
workbook = new HSSFWorkbook();
//create a new work sheet
sheet = workbook.createSheet("Test Result");
testresultdata = new LinkedHashMap < String, Object[] > ();
//add test result excel file column header
//write the header in the first row
testresultdata.put("1", new Object[] {
"Test Step Id", "Action", "Expected Result", "Actual Result"
});
}
#AfterClass:
#AfterClass
public void setupAfterSuite(ITestContext context) {
//write excel file and file name is TestResult.xls
Set<String> keyset = testresultdata.keySet();
int rownum = 0;
for (String key : keyset) {
Row row = sheet.createRow(rownum++);
Object [] objArr = testresultdata.get(key);
int cellnum = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellnum++);
if(obj instanceof Date)
cell.setCellValue((Date)obj);
else if(obj instanceof Boolean)
cell.setCellValue((Boolean)obj);
else if(obj instanceof String)
cell.setCellValue((String)obj);
else if(obj instanceof Double)
cell.setCellValue((Double)obj);
}
}
try {
FileOutputStream out =new FileOutputStream(new File("C:/Users/PathToFile/LoginCombinations.xls"));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
#Test blocks:
#Test(priority=0)
public void successfulLogin() throws InterruptedException {
Properties prop = new Properties();
InputStream config = null;
InputStream signinpage;
try {
// First we iterate over and read the config file
config = new FileInputStream("C:/Users/PathToFile/src/ConfigFiles/config");
prop.load(config);
signinpage = new FileInputStream("C:/Users/PathToFile/src/ObjectRepositories/signinpage");
prop.load(signinpage);
// Next we initiate the driver, and navigate to the Web Application
driver = new FirefoxDriver();
driver.get(prop.getProperty("url"));
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Now we run the first step, "enterValidCredentials"
// In this test, this is actually the only step.
LoginPage.enterValidCredentials.run(driver);
// Assert that we landed on the Product Select page.
// assertEquals(driver.findElement(By.xpath(prop.getProperty("tempproductselect"))).getText(), "SELECT PRODUCT");
try{
assertEquals(driver.findElement(By.xpath(prop.getProperty("tempproductselect"))).getText(), "SELECT PRODUCT");
//add pass entry to the excel sheet
testresultdata.put("2", new Object[] {1d, "User can login with a valid username and password", "Login successful","Pass"});
}
catch(Exception e)
{
//add fail entry to the excel sheet
testresultdata.put("2", new Object[] {1d, "User can login with a valid username and password", "Login successful","Fail"});
}
// Write the test result to the sheet.
driver.close();
Alert alert = driver.switchTo().alert();
alert.accept();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (config != null) {
try {
config.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
#Test(priority=1)
public void invalidCredentialsOne() throws InterruptedException {
Properties prop = new Properties();
InputStream config = null;
InputStream signinpage;
try {
// First we iterate over and read the config file
config = new FileInputStream("C:/Users/PathToFile/src/ConfigFiles/config");
prop.load(config);
signinpage = new FileInputStream("C:/Users/PathToFile/src/ObjectRepositories/signinpage");
prop.load(signinpage);
// Next we initiate the driver, and navigate to the Web Application
WebDriver driver;
driver = new FirefoxDriver();
driver.get(prop.getProperty("url"));
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Now we run the first step, "invalidCredentialsOne"
// In this test, this is actually the only step.
LoginPage.invalidCredentialsOne.run(driver);
Thread.sleep(5000);
try{
assertEquals(driver.findElement(By.xpath(prop.getProperty("failedlogin"))).getText(), "LOG IN");
//add pass entry to the excel sheet
testresultdata.put("3", new Object[] {2d, "User should not be able to login with an invalid password", "Login failed","Pass"});
}
catch(Exception e)
{
//add fail entry to the excel sheet
testresultdata.put("3", new Object[] {2d, "User should not be able to login with an invalid password", "Login failed","Fail"});
}
// Write the test result to the sheet.
// After the test, we close the driver.
driver.close();
Alert alert = driver.switchTo().alert();
alert.accept();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (config != null) {
try {
config.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The second test, invalidCredentialsOne, never writes to the excel, whether I make it pass or fail.
Java is also new to me, so forgive any formatting/lingo/whatever errors I have in there. I'm very open-minded to suggestion, I'm trying to improve.
Here is the structure as i see it:
1) A part where you have a DriverFactory defined.
public class BrowserFactory {
public static WebDriver localDriver(Capabilities capabilities) {
String browserType = capabilities.getBrowserName();
if (browserType.equals("firefox"))
return new FirefoxDriver(capabilities);
if (browserType.startsWith("internet explorer"))
return new InternetExplorerDriver(capabilities);
if (browserType.equals("chrome"))
return new ChromeDriver(capabilities);
throw new Error("Unrecognized browser type: " + browserType);
}
Then you can simply initialize it anytime you need it :
Example:
driver = BrowserFactory.localDriver(DesiredCapabilities.firefox());
2) Your test classes, where you use this factory. Then there will be no need in #BeforeClass annotations. You write your tests in those classes. And at the end of every test, you make an assert (if test result failed or not). To check if the test passes, use the Assert.true();
Example: I i use the wrokg credentials on login, the allert: Wrong Password will appear.
Solution: You make an Assert.true(errorMessagePresent)
3) Your output writer class - to make it accessible for your tests
3) In case the test passes - you add the string you want to the output, using the buffer reader, else you throw an exception
I am trying to use Selenium with JUnit and I am having trouble completing my tests because it seems like my button execution is only occurring once. here's some of the code:
JQueryUITab navTab = new JQueryUITab(driver.findElement(By.cssSelector("nav ul.tabs")));
try {
navTab.selectTab("Tab1");
} catch (Exception e) {
e.printStackTrace();
}
try {
navTab.selectTab("Tab2");
} catch (Exception e) {
e.printStackTrace();
}
System.out.print(navTab.getSelectedTab());
the console print out will read "Tab1". this JQueryUITab object is a custom object. here are the inner workings:
public String getSelectedTab() {
List<WebElement> tabs = jQueryUITab.findElements(By.cssSelector("li.tab"));
for (WebElement tab : tabs) {
if (tab.getAttribute("class").equals("tab selected")) {
return tab.getText();
}
}
return null;
}
public void selectTab(String tabName) throws Exception {
boolean found = false;
List<WebElement> tabs = jQueryUITab.findElements(By.cssSelector("li.tab"));
for (WebElement tab : tabs) {
if(tabName.equals(tab.getText().toString())) {
tab.click();
found = true;
break;
}
}
if (!found) {
throw new Exception("Could not find tab '" + tabName + "'");
}
}
There are no exceptions thrown. At least pertaining before or at this part of the code.
There were a couple problems wrong with my implementation. Firstly, it could have been improved by selecting not the li.tab object, but the a class inside of it. From there, there were 2 solutions that worked for me. First was using
webElement.sendKeys(Keys.ENTER);
and the second (imho more elegant solution) was to get the instance of the selenium driver object controlling the object and then get it to execute the command to click the tab. Here's the full corrected method.
public void selectTab(String tabName) throws Exception {
boolean found = false;
List<WebElement> tabs = jQueryUITab.findElements(By.cssSelector("li.tab a"));
for (WebElement tab : tabs) {
if(tabName.equals(tab.getText().toString())) {
// tab.sendKeys(Keys.ENTER);
WrapsDriver wrappedElement = (WrapsDriver) jQueryUITab;
JavascriptExecutor driver = (JavascriptExecutor) wrappedElement.getWrappedDriver();
driver.executeScript("$(arguments[0]).click();", tab);
found = true;
break;
}
}
if (!found) {
throw new Exception("Could not find tab '" + tabName + "'");
}
}