I have a test in which I have a Alertdialog on which there is an "input" field and buttons "Cancel"(id is button2) and "Ok"(id is button1). First I have to enter the value "1234" in the field, and then click on the "Ok" button. But it doesn’t work for me, the test fails.
onView(withId(R.id.input)).perform(typeText("1234"));
closeSoftKeyboard();
click(R.id.button1);
Thread.sleep(5000);
You should use the the isDialog() RootMatcher:
onView(allOf(
isDescendantOfA(withId(R.id.input)),
isAssignableFrom(EditText.class)))
.inRoot(isDialog())
.perform(typeText("1234"))
.perform(closeSoftKeyboard());
onView(withText("Ok"))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(click());
Thread.sleep(5000);
Related
I have a Selenium script to execute the following steps:
Launch the website - https://vusevapor.com/
Hit "I am 21+" button
Move to element devices.
Right-click on ciro complete kit.
The issue is that the right-click menu remains as is, and the element is getting clicked on the same page.
Here is my code:
//website
driver.get("https://vusevapor.com/");
//clicking on i am 21+ button
driver.findElement(By.xpath("/html/body/aside/div[2]/div/div/div[2]/div/a[1]/span")).click();
Thread.sleep(5000);
//xpath of devices menu
WebElement devices = driver.findElement(By.xpath("//*[#id=\"store.menu\"]/nav/ul/li[2]/a/span"));
//move to element devices
Actions act = new Actions(driver);
act.moveToElement(devices).build().perform();
Thread.sleep(3000);
//xpath of ciro complete kit
WebElement ciroKit = driver.findElement(By.xpath("//*[#id=\"store.menu\"]/nav/ul/li[2]/ul/li[2]/ul/li[1]/a/span"));
//right click on ciro complete kit
//*****Issue********right click is happening but the element is getting clicked on the same tab and right click menu remains as is
act.contextClick(ciroKit).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
try this:
option 1:
act.contextClick(ciroKit).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
option 2-
Use Action and Robot class:
act.contextClick(ciroKit).build().perform();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_ENTER);
Hope this helps.
WebDriver driver = new ChromeDriver();
WebElement radmale = driver.findElement(By.xpath("//label[#for='RESULT_RadioButton-8_0']"));
System.out.println(radmale.isDisplayed());
System.out.println(radmale.isEnabled());
System.out.println("Before selecting radio button , the status is:" + radmale.isSelected());
radmale.click();
// After click should be true, but still false why ??????
System.out.println("After selecting radio button , the status is:" + radmale.isSelected());
You are checking whether the label is selected, however you should rather be checking the associated input instead.
So you need to introduce one more WebElement - for the radio input. It would be also good to execute Element.scrollIntoView() function in order to get confidence that the element is visible and can be interacted with.
Suggested updated code:
driver.get("https://fs2.formsite.com/meherpavan/form2/index.html");
WebElement radmale = driver.findElement(By.xpath("//label[#for='RESULT_RadioButton-8_0']"));
WebElement radmaleInput = driver.findElement(By.id("RESULT_RadioButton-8_0"));
System.out.println(radmale.isDisplayed());
System.out.println(radmale.isEnabled());
System.out.println("Before selecting radio button , the status is:" + radmaleInput.isSelected());
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", radmale);
radmale.click();
// After click should be true, but still false why ??????
System.out.println("After selecting radio button , the status is:" + radmaleInput.isSelected());
Also be aware that it's recommended to go for Page Object Model design pattern - this way you will be able to split test logic and DOM elements definitions hence your test code will always remain the same as it will be independent from the GUI part.
I am trying to automate a scenario where I am encountering a window modal dialog box. Please let me know how to automate this situation? I just want to know how to click the highlighted OK button on the popup appearing? Please suggest
Try this
driver.switchTo().alert().accept();
You can also send keyboard events to press enter key as soon as pop up is active
Pressing enter key is as equivalent as clicking OK button
Make use of Robot class in java
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER)
You can try this by using JavascriptExecutor. It always works if we fail to find element using findelement method
I just found a way to handle this issue.
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
d = new FirefoxDriver(dc);
then implemented the alert code in try catch block
try
{
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println("Alert data: " + alertText);
alert.accept();
}
catch (UnhandledAlertException e)
{
e.printStackTrace();
}
Here i'm using linkedin for the learning purpose & want to perform the action on Edit profile=> work experience=> Add position. Here "Add position" button comes under work experience which becomes visible on hovering to that specific area. Look at my code,
driver.findElement(By.id("login-email")).clear();
driver.findElement(By.id("login email")).sendKeys("email id");
driver.findElement(By.id("login-password")).clear();
driver.findElement(By.id("login-password")).sendKeys("password");
driver.findElement(By.name("submit")).click();
driver.findElement(By.linkText("Home")).click();
driver.findElement(By.linkText("Profile")).click();
Thread.sleep(3000);
/* To scroll the page down */
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");
/* To hover mouse on required option*/
Actions hover=new Actions(driver);
hover.moveToElement(driver.findElement(By.xpath("id('background-experience')/div[1]/div/button[1]")));
driver.findElement(By.xpath("id('background-experience')/div[1]/div/button[1]")).click();
Thread.sleep(5000);
/* To Edit company name */
driver.findElement(By.xpath("id('companyName-positionCompany-position-editPositionForm')")).clear();
driver.findElement(By.xpath("id('companyName-positionCompany-position-editPositionForm')")).sendKeys("testing company");
This is so on continued to edit work details but when i execute, it stops the page at work experience & not able to click on add position button to edit work.
It gives error on "edit company" code line. Can anyone help me to perform the same?
//Hover
Actions actions = new Actions(driver);
IWebElement menuHoverLink = driver.FindElement(By.XPath("//*[#id='background-experience']/div[1]/div/button[1]"));
actions.MoveToElement(menuHoverLink);
actions.Build().Perform();
driver.FindElement(By.XPath("//*[#id='background-experience']/div[1]/div/button[1]")).Click();
I'm new to automation testing. I'm using twitter as a test website to learn selenium.
I want to create a new tweet and for doing that I'm using the below scenario. I'm able to open the 'New tweet' pop-up but still can't enter text in its text box. Please help.
Pre-Requisite: Login into Twitter
Click on the button (for creating new tweet) on top right side of screen.
The pop up opens up
Click on the text box present on pop-up to enter text.
The cursor gets highlighted in text box and save button enables
Enter some text
Click on Save button
For me it's working till point 2 and after that it doesn't enter text in textbox
See my code below:
String popUpHandle = driver.getWindowHandle();
driver.switchTo().window(popUpHandle);
driver.findElement(By.id("global-new-tweet-button")).click();
driver.findElement(By.xpath("(//button[#type='button'])[123]")).click();
driver.findElement(By.xpath("(//button[#type='button'])[123]")).sendKeys("test tweet");
driver.findElement(By.xpath("//button[#type='button'])[106]")).click();
#user3241182
I just tested this myself. You do not need to use any window handling.
Code Below is a provided solution and works great.
WebDriver driver = new FirefoxDriver();
driver.get("https://twitter.com/");
WebElement username = driver.findElement(By.id("signin-email"));
username.clear();
username.sendKeys("your_twitter_handle");
WebElement pass = driver.findElement(By.id("signin-password"));
pass.clear();
pass.sendKeys("your_twitter_passwd");
WebElement signInButton = driver.findElement(By.xpath("/html/body/div/div[2]/div/div[2]/div[2]/div[2]/form/table/tbody/tr/td[2]/button"));
signInButton.click();
WebElement tweetButton = driver.findElement(By.id("global-new-tweet-button"));
tweetButton.click();
WebElement tweetBox = driver.findElement(By.id("tweet-box-global"));
tweetBox.clear();
tweetBox.sendKeys("This is an automated tweet!");
WebElement tweetSubmit = driver.findElement(By.xpath("/html/body/div[9]/div[2]/div[2]/div[2]/form/div[2]/div[2]/button"));
tweetSubmit.click();