How to click “ok” in popup “Message from webpage” in selenium c#? - selenium

I have an application in which after clicking button, a popup comes titled Message from Webpage showing the error message.I want to click Ok button on it and verify the error message.
I tried the following code
public void clickButtonWithOk()
{
try
{
Thread.Sleep(Convert.ToInt32(GlobalVariables.WaitTime.ToString()) * 1000);
// Get element at runtime
inputData = "45";
waitForElementDisplayed();
int runTimeObjectSize = driver.FindElements(getApplicationObject(currentObjectName, currentObjectType, currentObjectUniqueId)).Count;
if (runTimeObjectSize == 0)
STEPLogger.logmessage(STEPLogger.FAIL, "FAILED to click button " + currentObjectName + " Object NOT Found");
else
{
IWebElement element = driver.FindElements(getApplicationObject(currentObjectName, currentObjectType, currentObjectUniqueId))[0];
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
executor.ExecuteScript("window.confirm = function(msg){return true;};");
executor.ExecuteScript("arguments[0].click();", element);
STEPLogger.logmessage(STEPLogger.PASS, "SUCCESSFULLY clicked on button " + currentObjectName);
}
}
catch (Exception oException)
{
STEPLogger.logmessage(STEPLogger.FAIL, "FAILED clickButton:" + oException.ToString());
}
} // End of CLICKBUTTONWithOk

Related

Apply wait for button change to text and again text change to button in selenium

I'm working on application that on [RUN] button click will change text to i.e [QUEUE].
After page refresh, button will have text i.e [RUNNING]. Again after refresh page, button will have text [VIEW]. Waiting time is dynamic and can vary.
User should see update through page refresh.
[RUN] --> QUEUE --> RUNNING --> [VIEW].
I have following code:
for(int i=1; i<11; i++) {
String fileNames=driver.findElement(By.xpath("/html[1]/body[1]/div[2]/div[3]/div[1]/table[1]/tbody[1]/tr["+ i +"]/td[1]")).getText();
System.out.println("file names: " +fileNames);
if(fileNames.equalsIgnoreCase("AlgeriaTest005")) {
String colText=driver.findElement(By.xpath("/html[1]/body[1]/div[2]/div[3]/div[1]/table[1]/tbody[1]/tr["+ i +"]/td[3]")).getText();
if(colText.equalsIgnoreCase("RUN")) {
driver.findElement(By.xpath("/html[1]/body[1]/div[2]/div[3]/div[1]/table[1]/tbody[1]/tr["+ i +"]/td[3]/button[1]")).click();
WebElement dropDown=driver.findElement(By.xpath(" /html/body/div[3]/div/div[2]/select"));
Select select=new Select(dropDown);
select.selectByVisibleText("NoReinsurance");
driver.findElement(By.xpath("/html/body/div[3]/div/div[3]/button[1]")).click();
//js.executeScript("arguments[0].scrollIntoView(true)", showEle); //it will scroll again..
String colText2=driver.findElement(By.xpath("/html[1]/body[1]/div[2]/div[3]/div[1]/table[1]/tbody[1]/tr["+ i +"]/td[3]")).getText();
if(colText2.equalsIgnoreCase("QUEUE")) {
System.out.println("File is changed to Queue..");
driver.navigate().refresh();
wait=new WebDriverWait(driver, Duration.ofMinutes(1));
//driver.navigate().refresh();
wait.until(ExpectedConditions.textToBePresentInElement( driver.findElement(By.xpath("/html[1]/body[1]/div[2]/div[3]/div[1]/table[1]/tbody[1]/tr["+ i +"]/td[3]")), "RUNNING"));
driver.navigate().refresh();
String colText3=driver.findElement(By.xpath("/html[1]/body[1]/div[2]/div[3]/div[1]/table[1]/tbody[1]/tr["+ i +"]/td[3]")).getText();
if(colText3.equalsIgnoreCase("RUNNING")) {
System.out.println("File is changed to Running..");
wait=new WebDriverWait(driver, Duration.ofMinutes(5));
driver.navigate().refresh();
wait.until(ExpectedConditions.textToBePresentInElement(
driver.findElement(By.xpath("/html[1]/body[1]/div[2]/div[3]/div[1]/table[1]/tbody[1]/tr["+ i +"]/td[3]/button[1]")), "VIEW"));
driver.navigate().refresh();
System.out.println("File is changed to view");
break;
}
else
System.out.println("file is not changed to Running.."); Assert.assertTrue(false);
}
else
System.out.println("file is not changed to Queue.."); Assert.assertTrue(false);
}
else if(colText.equalsIgnoreCase("Damage file not available ")) {
System.out.println("file is damaged..");
Assert.assertTrue(false);
}
}
}

Sometimes not able to get current url in mobile chromium using selenium webdriver

Below is my code. Sometimes driver.getCurrentUrl() returns "about:blank" in that case I am trying to get current url using javascriptexecutor but it also returns "about:blank".
Correct domain name is getting but I need current url and not domain.
Tried with wait, page refresh and javascriptexecutor options as shown in code. But no luck. Is there any other way to get right current url everytime and not "about:blank"?
public void verifyLinks(WebDriver driver, String mainPage, String expectedUrl) {
try {
JavascriptExecutor js = (JavascriptExecutor) driver;
Set<String> a = driver.getWindowHandles();
System.out.println("window size- " + a.size());
if (a.size() > 1) {
switchToNextWindow(driver);
Thread.sleep(1000);
if (driver.getCurrentUrl().equals("about:blank")) {
//driver.navigate().refresh();
//js.executeScript("return document.readyState").toString().equals("complete");
String DomainUsingJS = (String) js.executeScript("return document.domain");
String browserUrl = (String) js.executeScript("return window.top.location.href.toString()");
String url = js.executeScript("return document.URL;").toString();
System.out.println("Domain-" + DomainUsingJS);
System.out.println("Browser url- " + browserUrl);
System.out.println("url- " + url);
}
//softAssert.assertEquals(driver.getCurrentUrl(), expectedUrl);
System.out.println("GetCurrenturl- " + driver.getCurrentUrl());
driver.close();
driver.switchTo().window(mainPage);
} else {
Thread.sleep(500);
driver.navigate().refresh();
System.out.println(driver.getCurrentUrl());
// softAssert.assertEquals(uiDriver().getDriver().getCurrentUrl(), expectedUrl);
driver.navigate().back();
Thread.sleep(500);
}
} catch (Exception e) {
e.printStackTrace();
}
}

Get dropdown values using actions class

After moving to the value which is present in dropdown, i want to get it using actions class. Below is the code i have written. i am trying to print the dropdown values. HTML tag for dropdown is input(for select i have code). Please help me
public static void caseSearch()
{
try
{
Actions a=new Actions(driver);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
logger.info("clicking on cases tab :: ");
driver.findElement(By.xpath(loader.getProperty(Constants.CaseTab))).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement ele=driver.findElement(By.xpath(loader.getProperty(Constants.CaseSearch)));
ele.click();
for(int i=0;i<=20;i++)
{
//i want to print the first value of dropdown in console
a.sendKeys(Keys.DOWN,Keys.DOWN,Keys.DOWN).build().perform();
String value=ele.getText();
System.out.println("value is = "+value);
a.sendKeys(Keys.DOWN,Keys.DOWN).build().perform();
Thread.sleep(3000);
a.sendKeys(Keys.ENTER).build().perform();
}
}
catch(Exception e)
{
logger.info("case search method is not executed :: " +e);
}
}
I think you forgot to get the value from the dropdown box.
You need to use the Select Class to get it.
Actions key = new Actions(browser);
WebElement dropdownElement = this.browser.findElement(By.xpath("YOUR DROPDOWN ELEMENT"));
Select dropOptions = new Select(dropdownElement);
int elements = dropOptions.getOptions().size();
for (int i = 0; i < elements; i++) {
dropdownElement.click();
if (i > 0) {
Thread.sleep(1000L);
key.sendKeys(Keys.DOWN).build().perform();
Thread.sleep(1000L);
key.sendKeys(Keys.ENTER).build().perform();
}
else {
Thread.sleep(1000L);
ActionPerformers.sendActionKey(browser, Keys.ENTER);
}
System.out.println("ELEMENT " + i + " -> " + dropOptions.getFirstSelectedOption().getText()); //Here you will get the selected Value...
}
This is only an exemple, I hope it suits you.

Verify outlook opened using selenium

I am using Selenium to test web application.
I want to test "Send email" button.
When I select some text on the screen and click on the button it opens
up Outlook Message with the selected text as the message of the email.
How can I verify the Outlook was opened and message contains that text and how can I close the outlook after the test is done?
You can take the help of Java. Can write a method like this... Evalue is the string that you are capturing from the WebElement using getText()
public static String getClipboard(String Evalue)
throws InterruptedException, AWTException {
Robot robot = new Robot();
Thread.sleep(30000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(30000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(30000);
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard()
.getContents(null);
try {
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String wintext = (String) t
.getTransferData(DataFlavor.stringFlavor);
System.out.println("wintext" + wintext);
if ("".equalsIgnoreCase(wintext.trim())) {
System.out.println("text is identical");
} else {
System.out.println("text is identical");
System.out.println(wintext);
}
// return text.trim();
}
} catch (Exception e) {
}
}

MessageDialog not closing

I'm using the following code to show a message dialog in my application :
MessageDialog dialog = new MessageDialog(null,
DialogFlags.Modal,
MessageType.Error,
ButtonsType.Ok,
"An error occured: " );
dialog.Run();
Problem is the Ok button on the window doesn't do anything.... The window only disappears when i hit the X button on top right corner.
Any ideas?
You need to call dialog.Destroy(); after your call to dialog.Run();
You can also hook the Response event to get notified of a button click:
var dialog = new MessageDialog (this,
DialogFlags.Modal,
MessageType.Info,
ButtonsType.YesNo,
"The Hulk could totally take Super Man");
dialog.Response += (object o, ResponseArgs args) => {
if (args.ResponseId == ResponseType.Yes) {
Console.WriteLine("Yes clicked");
} else if (args.ResponseId == ResponseType.No) {
Console.WriteLine("No clicked");
} else if (args.ResponseId == ResponseType.DeleteEvent) {
Console.WriteLine("Dialog closed without clicking a button");
}
dialog.Destroy();
};
dialog.Run();