public class listenerdemo {
#Test
public void login() {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");
System.out.println(" The Page Title...." + driver.getTitle());
System.out.println("The Page URL...." + driver.getCurrentUrl());
WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(500));
wait.until(ExpectedConditions. visibilityOfAllElementsLocatedBy (By.xpath("//*[#id='webklipper-publisher-widget-container-notification-close-div']")));
driver.close();
}
}
popup
Related
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\EclipseWorkSpace\\Seleniumdownloads\\chrome\\chromedriver_win32\\chromedriver.exe");
//WebDriver driver = new ChromeDriver();
// Selenium 3.0 we need to down load Html unit driver.
// URL : https://github.com/SeleniumHQ/htmlunit-driver/releases
WebDriver driver = new HtmlUnitDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.freecrm.com/");
driver.findElement(By.xpath("//span[contains(text(),'Log In')]")).click();
System.out.println(" Before login" + driver.getTitle());
Thread.sleep(10000);
WebElement email = driver.findElement(By.xpath("//input[#name='email']"));
email.sendKeys("test32145#hotmail.com");
// driver.findElement(By.xpath("//input[#name='email']")).sendKeys("test2135#hotmail.com");
driver.findElement(By.xpath("//input[#name='password']")).sendKeys("SWeet$123");
driver.findElement(By.xpath("//div[#class='ui fluid large blue submit button']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//span[contains(text(),'Contacts')]")).click();
System.out.println(" after login" + driver.getTitle());
Thread.sleep(2000);
// read.sleep(millis);
driver.close();
}
I want to use an entire java class , to write a test script efficiently . Instead of rewriting it on the class again. Login.java from a diffeerent package from Create_PurchaseReceive.java. I want to use login.java so I wont rewrite in again on Create_purchaseReceive.java.
Tried searching on google and tried calling public void extendbase
login.java
package Login;
public class Login {
WebDriver driver;
//Open Brower
#BeforeTest
public void LoginWebSystem() {
driver = new ChromeDriver();
driver.get("http://localhost:82");
WebElement email = driver.findElement(By.id("login_username"));
email.sendKeys("superadmin");
System.out.println("Username Set");
WebElement password = driver.findElement(By.id("login_password"));
password.sendKeys("nelsoft121586");
System.out.println("Password Set");
WebElement login = driver.findElement(By.id("login_submit"));
login.click();
System.out.println("Login Button Clicked");
}
//If login is successful or failed
#Test (priority=1)
public void LoginAccount() {
String newUrl = driver.getCurrentUrl();
if(newUrl.equalsIgnoreCase("http://localhost:82/controlpanel.php")){
System.out.println("Login Success");
}
else {
System.out.println("Login Failed");
create_purchasereceive.java
package PurchaseModule;
public class Create_PurchaseReceive {
WebDriver driver;
//Open Brower
#BeforeTest
public void LoginWebSystem() {
driver = new ChromeDriver();
driver.get("http://localhost:82");
WebElement email = driver.findElement(By.id("login_username"));
email.sendKeys("superadmin");
System.out.println("Username Set");
WebElement password = driver.findElement(By.id("login_password"));
password.sendKeys("nelsoft121586");
System.out.println("Password Set");
WebElement login = driver.findElement(By.id("login_submit"));
login.click();
System.out.println("Login Button Clicked");
}
//If login is successful or failed
#Test (priority=1)
public void LoginAccount() {
String newUrl = driver.getCurrentUrl();
if(newUrl.equalsIgnoreCase("http://localhost:82/controlpanel.php")){
System.out.println("Login Success");
}
else {
System.out.println("Login Failed");
}
}
//Proceed to Purchase Order page
#Test (priority=2)
public void PurchaseReceivePage() {
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Purchase Receive")));
driver.findElement(By.partialLinkText("Purchase Receive")).click();
System.out.println("Successful in proceeding to Purchase Receive");
}
#Test (priority=3)
public void NewPurchaseRecieve() {
driver.findElement(By.className("bttn-imp-create")).click();
System.out.println("Successful in proceeding to Purchase_receive.php");
String newUrl1 = driver.getCurrentUrl();
if(newUrl1.equalsIgnoreCase("http://localhost:82/purchase.php")){
System.out.println("Successful in proceeding to Purchase Receive page ");
}
else {
System.out.println("Failed in proceeding to Purchase Receive page");
}
}
#Test (priority=4)
public void SelectInvoice() {
driver.findElement(By.id("select-request-invoice")).click();
System.out.println("Successful in clicking select invoice");
WebElement selectinvoice = driver.findElement(By.id("select-request-invoice"));
selectinvoice.click();
}
#Test (priority=6)
public void SearchInvoice() {
WebDriverWait waitdateFROM = new WebDriverWait(driver, 25);
waitdateFROM.until(ExpectedConditions.elementToBeClickable(By.id("date-purchase-from")));
WebElement dateFROM = driver.findElement(By.id("date-purchase-from"));
dateFROM.sendKeys(Keys.chord(Keys.CONTROL, "a"), "2019-09-04",Keys.ENTER);
System.out.println("Successful in changing the date from");
WebElement dateTO = driver.findElement(By.id("date-purchase-to"));
dateTO.sendKeys(Keys.chord(Keys.CONTROL, "a"), "2019-09-05",Keys.ENTER);
System.out.println("Successful in changing the date to");
WebDriverWait waitdateTO = new WebDriverWait(driver, 25);
waitdateTO.until(ExpectedConditions.elementToBeClickable(By.className("bttn-search")));
WebElement searchbutton = driver.findElement(By.className("bttn-search"));
searchbutton.click();
System.out.println("Successful in clicking searchinvoice");
}
}
I expect to use login.java for create_purchasereceive.java
You can extends login class into Create_PurchaseReceive as given below. It may solve your issue.
package PurchaseModule;
public class Create_PurchaseReceive extends Login {
}
New Tab is not opening in Selenium
import org.testng.annotations.Test;
public class SimpleTest {
#Test
public void TestMethod() throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:\\22November2017\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com/");
Thread.sleep(3000);
WebElement element=driver.findElement(By.linkText("Gmail"));
Thread.sleep(3000);
element.sendKeys(Keys.CONTROL,"t");
}
}
Please help me find the error
Here is working code..it opens Gmail into new tab and switch to it.
import org.testng.annotations.Test;
public class SimpleTest {
#Test
public void TestMethod() throws InterruptedException{
System.setProperty("webdriver.gecko.driver","C:\\22November2017\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com/");
Thread.sleep(3000);
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("Gmail")).sendKeys(selectLinkOpeninNewTab);
Thread.sleep(3000);
ArrayList<String> tab = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tab.get(1));
}
}
To open a New Tab with the linkText("Gmail") you can use the following code block :
String URL="http://www.google.com";
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get(URL);
System.out.println("Page Title is : "+driver.getTitle());
WebElement link = driver.findElement(By.linkText("Gmail"));
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).click(link).keyUp(Keys.CONTROL).build().perform();
open the link in new Tab.
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("http://www.google.com/")).sendKeys(selectLinkOpeninNewTab);
You can use JavaScriptExecutor to do it
Javascriptexecutor js = (Javascriptexecutor)driver;
js.executescript("var win = window.open('"+ YourURLHere + "', '_blank');win.focus(); ");
Above code will open new tab and navigate to the URL provided and focus on the new tab.
I'm unable to execute the code inside #test but it is execute in the #BeforeTest Annotation. It is open the driver which is in BeforeTest Annotation but it is unable to find the Element ("//*[#id='lst-ib']") which is in #Test and throws a NULL Exception.
package package2;
public class dataprovider {
WebDriver driver;
#BeforeTest
public void setup(){
System.setProperty("webdriver.chrome.driver","./drivers/chromedriver.exe/");
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://google.com/");
/*System.setProperty("webdriver.firefox.marionette", "./drivers/geckodriver.exe/");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");*/
}
/** Test case to verify google search box
* #param author
* #param searchKey
* #throws InterruptedException
*/
#Test(dataProvider="SearchProvider")
public void testMethod(String author,String searchKey) throws InterruptedException{
{
WebElement searchText = driver.findElement(By.xpath("//*[#id='lst-ib']"));
//search value in google searchbox
searchText.sendKeys(searchKey);
System.out.println("Welcome ->"+author+" Your search key is->"+searchKey);
Thread.sleep(3000);
String testValue = searchText.getAttribute("value");
System.out.println(testValue +"::::"+searchKey);
searchText.clear();
//Verify if the value in google search box is correct
Assert.assertTrue(testValue.equalsIgnoreCase(searchKey));
}
}
/**
* #return Object[][] where first column contains 'author'
* and second column contains 'searchKey'
*/
#DataProvider(name="SearchProvider")
public Object[][] getDataFromDataprovider(){
return new Object[][]
{
{ "Guru99", "India" },
{ "Krishna", "UK" },
{ "Bhupesh", "USA" }
};
}
}
Inside your #BeforeTest public void setup(){} you create an instance of
ChromeDriver driver = new ChromeDriver();
While in testMethod is used the one that is defined outside:
WebDriver driver;
Change
ChromeDriver driver = new ChromeDriver();
with
driver = new ChromeDriver();
Why is Gecko Driver (v0.17.0 - x64bit) not opening Browser?
Base Page / Method:
public BasePage loadUrl(String url) throws Exception {
driver.get(url);
return new BasePage(driver);
}
Cucumber Step:
#Given("^User navigates to the \"([^\"]*)\" website$")
public void user_navigates_to_the_website(String url) throws Throwable {
BasePage basePage = new BasePage(driver);
basePage.loadUrl(url);
}
Driver Factory:
public WebDriver getDriver() {
try {
if(driver == null){
System.setProperty("webdriver.gecko.driver", Constant.GECKO_DRIVER_DIRECTORY);
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
basePage = PageFactory.initElements(driver, BasePage.class);
loginPage = PageFactory.initElements(driver, LoginPage.class);
}
} catch (Exception e) {
}
return driver;
}
NEW CODE - Driver Factory: uses if statements to point to exe files for each browser type:
public WebDriver getDriver() {
try {
ReadConfigFile file = new ReadConfigFile();
if (driver == null) {
if("chrome".equalsIgnoreCase(file.getBrowser())){
System.setProperty("webdriver.chrome.driver", Constant.CHROME_DRIVER_DIRECTORY);
driver = new ChromeDriver();
}
if("firefox".equalsIgnoreCase(file.getBrowser())){
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
System.setProperty("webdriver.gecko.driver", Constant.GECKO_DRIVER_DIRECTORY);
capabilities.setCapability("marionette", true);
driver = new FirefoxDriver(capabilities);
}
if("ie".equalsIgnoreCase(file.getBrowser())){
System.setProperty("webdriver.ie.driver", Constant.IE_DRIVER_DIRECTORY);
driver = new InternetExplorerDriver();
}
}
}
fixed upgrading to Selenium version: 3.4.0