Entering text in pop up and saving it on TestNG - selenium

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();

Related

How do I track information about a button clicked on a website? (Selenium)

using OpenQA.Selenium.Firefox...
var driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://www.google.com");
string text = ...
IWebElement webElement = ...
After opening, the user does some manipulations, enters text, and presses a button.
Is it possible to track these actions using Selenium???
Maybe there are some other tools?

How to attach a file on Windows PC using Selenium WebDriver in Java

Manual steps:
Click Browse on the application Windows dialog box displays.
Enter file path in the file name field in windows dialog box.
Click the Open button
It attaches the file.
Code using the robot class:
#Test
public void test(){
WebElement element = driver.findElement(By.className("attach"));
element.click();
waitSeconds(9000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_1);
robot.keyPress(KeyEvent.VK_PERIOD);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_ENTER);}
When I run the test, it works just fine except for the last line.
Last line presses enter key just fine. After that, dialog box opens again. So file is never attached. Windows dialog box has two buttons: open and cancel. It should press Open button once after the file path is entered in the file name field. How can I do it?
Using org.openqa.selenium.JavascriptExecutor to set elements attribute 'value':
import org.openqa.selenium.JavascriptExecutor
//...
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
String script = "document.getElementsByClassName('attach')[0].setAttribute('value', 'C:\...\D1.txt');";
javascriptExecutor.executeScript(script);

Trying to hover on a menu and click on a link in the sub-menu but no luck

I am learning to hover over a menu and click on a link int he sub-menu.
The scenario is go to the URL "https://www.amazon.in", hover over "Hello Sign in" and then click on the link "Start here".
I can hover over "Hello Sign in" using moveToElement() and sub menu is opening, but couldn't click on the link "Start here".
Here is my code.
WebElement signUp = driver.findElement(By.id("nav-link-yourAccount"));
Actions action = new Actions(driver);
action.moveToElement(signUp).build().perform();
WebElement startHere =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Start here")));
startHere.click();
The link text includes a dot at the end, you missed it in your code, Try By.linkText("Start here.") or By.partialLinkText("Start here")
Please try the below modified code and it's working as expected. First you can find the new Customer Div and then directly access the start here link as below.
WebElement signUp = driver.findElement(By.id("nav-link-yourAccount"));
Actions action = new Actions(driver);
action.moveToElement(signUp).build().perform();
WebElement newCustomer=driver.findElement(By.id("nav-flyout-ya-newCust"));
newCustomer.findElement(By.xpath(".//a")).click();
To access the URL https://www.amazon.in then hover over Hello Sign in and then click on the link Start here. you have to induce WebDriverWait for the element with text as Hello Sign in to be visible, then Mouse Hover over it and then induce WebDriverWait for the element with text as Start here. to be clickable and you can use the following solution :
Code Block :
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.in/");
WebElement signUp = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("nav-link-yourAccount")));
new Actions(driver).moveToElement(signUp).build().perform();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.linkText("Start here."))).click();
Browser Snapshot :

How to click on a check box in a new window through selenium webdriver

I am trying to make this check box click but not I'm not able to click on check box.
Please send me the xpath or css
I tried:
xpath
//div//*[#id='thCheckBox']
.//*[#id='searchOrderModel']/div/div/div[3]/div/button[2]
There is a label next to the checkbox. You can try to click on the label then it will click the checkbox using below x-path.
//label[#for='thCheckbox']
As per the HTML you have provided and your question as the Check Box is on a new window to click() on the Check Box you have to induce WebDriverWait for the Check Box to be clickable and you can use the following Locator Strategy :
cssSelector :
"table.table.table-hover.dataTable#orderNoDropdown label[for='thCheckBox']"
xpath :
"//table[#class='table table-hover dataTable' and #id='orderNoDropdown']//label[#for='thCheckBox']"
As you have mentioned it is new window, then you will have to switch to new windows in order to interact with the web element. after reaching to new window you can use this code :
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
//Now you have focused on new Windows , and you can interact with check box now :
WebElement checkBox = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("thCheckBox")));
checkBox.click();
Then again you have to switch back to page from where you went to this Page, if you want that.
driver.close();
driver.switchTo.windows(tabs.get(0));

How to get a tooltip text on mouseover using Selenium WebDriver

I am not able to get tooltip text after mouseover on icon tooltip. I need to get tooltip text,This is html code.
<a class="tooltip" onclick="SocialAuth.showOpenIDLoginWindow('google');_gaq.push(['_trackEvent','LoginForm','google-login','google login was clicked']);" href="javascript:void(0);"><span>We dont store your password or access your contacts on your Google account.</span><img class="social" height="39" width="40" src="/images/login/google.png"/>
The method to get text from a tool tip differs from a HTML one when its a Jquery ToolTip.
getAttribute() does not work when its a Jquery ToolTip. If you see the tooltip example at http://demoqa.com/tooltip/ , its a jquery tooltip.
Following code works here:
WebDriver driver=new FirefoxDriver();
driver.get("http://demoqa.com/tooltip/");
WebElement element = driver.findElement(By.xpath(".//*[#id='age']"));
Actions toolAct = new Actions(driver);
toolAct.moveToElement(element).build().perform();
WebElement toolTipElement = driver.findElement(By.cssSelector(".ui-tooltip"));
String toolTipText = toolTipElement.getText();
System.out.println(toolTipText);
A good reference is:
http://www.seleniumeasy.com/selenium-tutorials/how-to-verify-tooltip-text-with-selenium-webdriver-using-java
Use the below line of code for fetching the tool tip text from the Element.
String toolTipText = driver.findElement(By.id(element's id)).getAttribute("title");
You have to use Actions for this. in this am printing the mouse hover message in Google
Actions ToolTip1 = new Actions(driver);
WebElement googleLogo = driver.findElement(By.xpath("//div[#id='hplogo']"));
Thread.sleep(2000);
ToolTip1.clickAndHold(googleLogo).perform();
Perform mouse hover action using 'clickAndHold' method.
Get the value of Tool tip by using 'getAttribute' command
String ToolTipText = googleLogo.getAttribute("title");
Assert.assertEquals(ToolTipText, "Google");
Thread.sleep(2000);
System.out.println("Tooltip value is: " + ToolTipText);