I created an app that lets users login through my app to Wikipedia, and I achieved this goal with selenium, but I can't find a way to verify either credentials are ok or not.
I tried find by id but since failed condition doesn't display an ID it's not helping.
private void button1_Click(object sender, EventArgs e)
{
string BaseUrl = "https://en.wikipedia.org/w/index.php?title=Special:UserLogin&returnto=Main+Page";
int TimeOut = 30;
var driver = new FirefoxDriver();
driver.Navigate().GoToUrl(BaseUrl);
var loginBox = driver.FindElement(By.Id("wpName1"));
loginBox.SendKeys("email.address#gmail.com");
var pwBox = driver.FindElement(By.Id("wpPassword1"));
pwBox.SendKeys("!SuperSecretpassw0rd");
I would like to know if entered credentials are correct or not.
A general approach to this kind of question is to ask yourself, "How can a human see this?" and then replicate this behavior in your test. In your example, how would a human detect that the login is wrong?
A human would see the error message.
Selenium on the other hand only sees the DOM tree. So for Selenium to see the error message, you need to find out where to look in the DOM tree. To do this, open your browser developer tools and find the matching section in the DOM tree:
With this in mind, a very simple solution is to find the error div that is shown when the credentials are invalid.
var error = driver.findElement(By.className("error"));
Then you can check if the element actually exists and you can use additional Selenium methods to inspect the actual contents of the error message, to see what the error is. If the field is not present then you could assume that the login succeeded. In addition you can use driver.getCurrentUrl() to inspect whether you are still located on the login page, to confirm that you are really logged in.
That being said, if you try to do any serious browser testing you should consider using the page object model (see https://www.toolsqa.com/selenium-webdriver/page-object-model/) so you don't end up with an unmaintainable mess of test cases.
As you havn't mentioned the language binding this solution is based on Java.
An elegant approach to validate whether the credentials are valid or not would be to induce a try-catch{} block to look for the element with the error inducing WebDriverWait for the desired visibilityOfElementLocated() and you can use the following Locator Strategies:
Code Block:
public class A_demo
{
public static void main(String[] args) throws Exception
{
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
WebDriver driver = new ChromeDriver(options);
driver.get("https://en.wikipedia.org/w/index.php?title=Special:UserLogin&returnto=Main+Page");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[name='wpName']"))).sendKeys("Helios.Lucifer#stackoverflow.com");
driver.findElement(By.cssSelector("input[name='wpPassword']")).sendKeys("!SuperSecretpassw0rd");
driver.findElement(By.cssSelector("button#wpLoginAttempt")).click();
try
{
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("form[name='userlogin']>div.error>p")));
System.out.println("Unsuccessful login attempt");
}
catch (TimeoutException e)
{
System.out.println("Successful Login.");
}
driver.quit();
}
}
Console Output;
Unsuccessful login attempt
Related
I can't find the error with my xpath element locator
Login using automated testing tool Selenium webdriver
driver.findElement(By.className("btn-primary")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[#aria-labelledby='vaadin-text-field-label-19']")).click();
driver.findElement(By.xpath("//input[#aria-labelledby='vaadin-text-field-label-19']")).sendKeys("xxx#gmail.com");
driver.findElement(By.xpath("//input[#type='password']")).click();
driver.findElement(By.xpath("//input[#type='password']")).sendKeys("12345678");
driver.findElement(By.className("btn-block")).click();
Try the following xpath to enter details.
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://jbm4u.com/jupiter/#/signin");
driver.findElement(By.className("btn-primary")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[#class='card']//vaadin-text-field[#placeholder='Enter email address']")).click();
driver.findElement(By.xpath("//div[#class='card']//vaadin-text-field[#placeholder='Enter email address']")).sendKeys("xxx#gmail.com");
driver.findElement(By.xpath("//div[#class='card']//vaadin-password-field[#placeholder='Enter Password']")).click();
driver.findElement(By.xpath("//div[#class='card']//vaadin-password-field[#placeholder='Enter Password']")).sendKeys("12345678");
}
Browser Snapshot
Can you please try with this xpath
driver.findElement(By.xpath("//input[#placeholder='Enter email address']")).click();
driver.findElement(By.xpath("//input[#placeholder='Enter email address']")).sendKeys("xxx#gmail.com");
Please avoid referring to attributes which has dynamic indexing in your example(vaadin-text-field-label-21) 21 is dynamically generated which tend to change for every time when you refresh or for a new session.
If you're wanting to fill in the email address you need this selector:
driver.findElement(By.cssSelector("vaadin-text-field[placeholder='Enter email address']")).sendKeys("test#email.com")
The password field is:
driver.findElement(By.cssSelector("vaadin-password-field[placeholder='Enter Password']")).sendKeys("dwedewdw")
And the submit button:
driver.findElement(By.cssSelector("button[class*='btn-block']")).click()
Try and avoid Xpath whenever possible, it's too flakey. I've tested the above selectors myself.
I am new to selenium web testing i have automated a Sign-In process for a web base application. now making it for Sign Up process i am stuck at a point where a verification code is sent to a mail address and then i have to copy that into my verification code field and proceed further
As i have searched so far i came to know about the mailosaur server but unable to copy that email verification code into my automated web browser. i also searched for the tutorials but unable to find any useful resource. also i want to generate random emails that part is also not getting in my mind.
As i am new to selenium so it is requested to please provide detail answer so i can understand it better, Thanks in advance, working on Intellij, Mavaen (Java)
You can use mailinator.com. No need to register or create a mail box. In your app just enter email with made up name #mailinator.com (asad1#mailinator.com, asadXY#mailinator.com, whatever).
To collect confirmation link (double opt in) I'm using this:
public class Mailinator {
public WebDriver driver;
public Mailinator(WebDriver driver) {this.driver = driver;}
public String urlMailinator = "https://www.mailinator.com/";
public WebDriverWait waitSec(WebDriver driver, int sec) {return new WebDriverWait(driver, sec);}
public static String doubleOptInLink = null;
public String getDoubleOptInLink() {return doubleOptInLink;}
public void setDoubleOptInLink (String doubleOptInLink) {Mailinator.doubleOptInLink = doubleOptInLink;}
public void collectDoubleOptInLink(String userEmail, int expectedNumberOfDeliveredEmails) throws InterruptedException {
driver.get(urlMailinator);
WebElement fldInbox = waitSec(driver, 5).until(ExpectedConditions.elementToBeClickable(By.id("inboxfield")));
fldInbox.sendKeys(userEmail);
WebElement btnGo = driver.findElement(By.xpath("/html/body/section[1]/div/div[3]/div[2]/div[2]/div[1]/span/button"));
btnGo.click();
waitSec(driver, 600).until(ExpectedConditions.numberOfElementsToBe((By.xpath("//*[#id=\"inboxpane\"]/div/div/div/table/tbody/tr")), expectedNumberOfDeliveredEmails));
WebElement lastMailLink = driver.findElement(By.xpath("//*[#id=\"inboxpane\"]/div/div/div/table/tbody/tr"));
lastMailLink.click();
Thread.sleep(3000);
driver.switchTo().frame(driver.findElement(By.id("msg_body")));
setDoubleOptInLink(driver.findElement(By.xpath("//*[#id=\"intro\"]/tbody/tr/td/a")).getAttribute("href"));
}
}
In my scenario:
register to webapp with new made up email, confirmation email is send
using collectDoubleOptInLink(email, 1); is the confirmation link set as doubleOptInLink
calling another method to go to the confirmation link with getDoubleOptInLink();
Sure you will need change what string comes to setDoubleOptInLink();
In specific cases don't forget to setDoubleOptInLink(null);.
Can anyone know that how can we handle Authenticate alert box of browser in selenium Webdriver ?
I user following code but its not working.
driver.switchTo().alert().authenticateUsing(new UserAndPassword("uname", "Password"));
Here is a screenshot of what I am trying to fill in:
Does anyone know how can I enter those credentials?
You can handle this in two ways:
You can pass the username and password directly through the URL like this:
driver..get("https://MyUserName:password#staging.www.abc.com")
You can also use AutoIT Tool for handling any kind of window popups.
For this you first have to download and install AutoIt
Then download SciTE4AutoIt3
You can do scripting in it, or you can use Au3Recorder. It is not available in new versions of SciTE, but you can download it from old versions separately. Unzip it and copy:
autoit-v3.3.14.0.zip\install\Extras\Au3Record
to
[#Install Dir of AutoIt in Program files]\Extras\Au3Record
Now you can start the recorder directly by clicking Au3Record.exe or you can find it in the Script Editor window Tools >AU3Recorder.
For it you have to create a blank .au3 file in the Script Editor. Now start recording. Perform action on Window Popup. Stop when Done. Save it with .au3 format.
Now GO to Saved File location >> Right Click on the File and compile it (Compile Script(x64) or else). It will create an .exe file in the same folder.Now run that script in your Project using:
Runtime.getRuntime().exec("File Loaction/MyAutoItScript.exe");
It will work.
Try below code:
String username = "myUsername";
String password = "myPassword";
String URL = "http://" + username + ":" + password + "#" + sso.mywebsite.com/usdf/ls/dia?kkkk;
driver.get(URL); // Basically operation done here itself still if not work use further Alert code as well
Alert alert = driver.switchTo().alert();
alert.accept();
Full code will be like:
driver.get("https://sso.mywebsite.com/usdf/ls/dia?kkkk");
String url = driver.getCurrentUrl().replaceAll("https://", "");
String username = "myUsername";
String password = "myPassword";
String URL = "https://" + username + ":" + password + "#" + url;
driver.get(URL); // Basically operation done here itself still if not work use further Alert code as well
Alert alert = driver.switchTo().alert();
alert.accept();
Note : Even alert code do not required .. use it as it works for you and this code works on chrome better
Something like this?
driver.Navigate().to("http://UserName:Password#url.com")
or
WebDriverWait wait = new WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword(*username*, *password*));
I use Java for my test automation. I have searched for a decent way to deal with these sign in pop ups and cannot find one. The most common answers are either to include in a URL as prefix prior to the real url (eg https:\username:password#www.website.com) or to use a wait for alert. These have not worked for me as: on a form submit there is no simple url to use and I am not sure as to the security including the password in the start of the url; with the wait for alert the webdriver hangs until there is a response - which only comes from submitting the login response via the pop up.
The workaround I have found is poor - I've not got it to work in a headless environment and so limits this answers usefulness. Would be great to get a real answer here. Note that I am running this in a Windows environment and if I was using Linux I have read that I could use xvfb to provide a 'screen' for sikuli and then this would work - if anyone can comment on how to do this on a Windows server that would be MUCH appreciated.
I use Sikuli for the automation of things I cannot automate via Selenium. Sikuli does many things, including letting you basically feed it images that it performs actions on.
For this purpose I run Sikuli on a thread started prior to clickin the submit that leads to the sign in pop-up. As it is running on a different thread it doesn't block the main thread, so it can still execute the log in. Once it logs in it shuts down and logging in closes the pop up and reactivates the main thread.
Specifically:
Sikuli MAVEN entry for POM:
<dependency>
<groupId>com.sikulix</groupId>
<artifactId>sikulixapi</artifactId>
<version>1.1.4-20191010.160947-268</version>
</dependency>
In the main code use a runnable executed via an executor:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
private final AtomicInteger expectedResultCount;
private final AtomicInteger publishedResultCount;
private final ExecutorService executor;
ExecutorService executor = Executors.newFixedThreadPool(5);
String processName = "asic-login";
LoginPopUp login = new LoginPopUp(this, processName);
addResultExpectation(processName);
executor.execute(login);
The runnable here implements an interface I use to keep things tidy:
The main class implements the interface to manage the threads:
public class TestRunner implements ResultPublisher{
These are functions inside the main class for thread management:
private void addResultExpectation(String process){
resultMap.put(process, new JSONObject());
expectedResultCount.addAndGet(1);
}
public void publishResult(JSONObject result){
String process = result.getString("process-name");
String strResult = result.getString("result");
resultMap.put(process, result);
publishedResultCount.addAndGet(1);
if(publishedResultCount.get() == expectedResultCount.get()){
executor.shutdown();
System.out.println("shutting down executor for run " + runId);
}
}
This is the interface
import org.json.JSONObject;
public interface ResultPublisher {
void publishResult(JSONObject result);
}
This is the runnable Runnable - an inner class in the TestRunner main class:
private class LoginPopUp implements Runnable{
private ResultPublisher publisher;
private String filePath;
private String processName;
private LoginPopUp(){
}
public LoginPopUp(ResultPublisher publisher, String processName){
this.publisher = publisher;
this.processName = processName;
}
private void publish(JSONObject result){
publisher.publishResult(result);
}
public void run(){
JSONObject result = new JSONObject();
result.put("path", filePath);
try{
Screen sd = new Screen();
ScreenUtility s = new ScreenUtility(imagesDirectory);
s.clickImage("LoginTitle.PNG", 10, 2500);
s.typeImageWithOffset("UserName.PNG", userName, 30,0);
s.typeImageWithOffset("Password.PNG",String.valueOf(password), 50,0);
s.clickImage("AsicSignIn.PNG", 10, 250);
}catch(Exception ex){
result.put("result", ex.getMessage());
result.put("process-name", processName);
publish(result);
Logger.getLogger(BCSRobot.class.getName()).log(Level.SEVERE, null, ex);
return;
}
result.put("result", "logged in successfully");
result.put("process-name", processName);
publish(result);
return;
}
}
I am trying to learn selenium webdriver automation but I am finding that the sendKeys command is not typing on Password type fields. I can see that some other people are also experiencing the same problem by googling it, but I haven't seen any correct answer yet. Could anyone please help me here.
Please find below sample code; I generated code from Selenium IDE and its working fine on IDE but not when I use webdriver.
package com.example.tests;
public class Login {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.webs.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testLogin() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.cssSelector("span")).click();
driver.findElement(By.id("FWloginUsername")).clear();
driver.findElement(By.id("FWloginUsername")).sendKeys("aug2qatestingqa#yahoo.com");
driver.findElement(By.id("FWloginPassword2")).clear();
driver.findElement(By.id("FWloginPassword2")).sendKeys("webs");
driver.findElement(By.id("sign_in_leaf")).click();
}
There were two password fields and one is hidden. Solution is to click on first password [hidden] field to get second password field enabled.
driver.findElement(By.id("FWloginUsername")).sendKeys("aug2qatestingqa#yahoo.com");
driver.findElement(By.id("FWloginPassword")).click();
driver.findElement(By.id("FWloginPassword2")).clear();
driver.findElement(By.id("FWloginPassword2")).sendKeys("webs");
I had almost a similar situation for Password field. There were two elements for the same 'Password' field but with different IDs. The JavaScript was toggling "type = password" on run time for a click, clear or any action to this field.
Solution in this case is to find the text with input type = password,
for example:
driver.FindElement(By.CssSelector("input[type='password']")).SendKeys(IWebElement);
My problem was that I used ActionChains which caused the later fields not being filled when using send_keys method.
The solution was to call actions.reset_actions()
e.g.
actions = ActionChains(driver)
actions.key_down(Keys.LEFT_CONTROL).send_keys("a").perform()
actions.key_down(Keys.LEFT_CONTROL).send_keys("c").perform()
actions.reset_actions()
# now send_keys() method works again
cvvTxtBox().sendKeys("1234");
cvvTxtBox().sendKeys(Keys.TAB);
Final Solution on this problem.
Else use Robot
lib- selenium-java2.0rc2.jar and selenium-server-standalone-2.-b3.jar
simple test:
webDriver = new FirefoxDriver();
webDriver.get("http://www.google.com");
webDriver.findElement(By.name("q")).sendKeys("Test");
webDriver.findElement(By.name("btnG")).click();
Assert.assertTrue(webDriver.findElement(By.cssSelector("ol#rso>li:nth-child(1) a"))
.getText().equalsIgnoreCase("Test.com Web Based Testing and Certification Software v2.0"));
Assertion fails, and then I added BAD wait statement, just before asserition -
Thread.sleep(3000);
Assert.assertTrue(webDriver.findElement(By.cssSelector("ol#rso>li:nth-child(1) a"))
.getText().equalsIgnoreCase("Test.com Web Based Testing and Certification Software v2.0"));
Test succeeds.
But then came across implicitlyWait and used it as -
webDriver = new FirefoxDriver();
webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
webDriver.get("http://www.google.com");
webDriver.findElement(By.name("q")).sendKeys("Test");
webDriver.findElement(By.name("btnG")).click();
Assert.assertTrue(webDriver.findElement(By.cssSelector("ol#rso>li:nth-child(1) a"))
.getText().equalsIgnoreCase("Test.com Web Based Testing and Certification Software v2.0"));
Assertion again fails, looks like implicitlyWait() does not have any impact here. And I definitely don't want to use Thread.sleep(). What could be the possible solution?
You can use webdriverwait class to wait for an expected condition to come true.
ExpectedCondition e = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
//Wait until text changes
return webDriver.findElement(By.cssSelector("ol#rso>li:nth-child(1) a"))
.getText().equalsIgnoreCase("Test.com Web Based Testing and Certification Software v2.0"));
}
};
Wait w = new WebDriverWait(driver, 60);
w.until(e);
//do asserts now
Assert.assertTrue(...);