I have a button with the follow html:
<span ng-show="!IsGoogleLinked()" aria-hidden="false" class="">Link Google</span>
<span ng-show="IsGoogleLinked()" aria-hidden="true" class="ng-hide">UnLink Google</span>
When you click this button and it goes through the linking process the button changes text from "Link Google" to "Unlink Google". I want to use the webdriver in such a way that'll check if the text in the button is "Unlink Google" it will click the button, and I'm missing a piece I hope someone could provide.
Here is the updated version using individual browser selection:
#When("^I check if the user has a \"([^\"]*)\" linked account$")
public void linkChecker(String socialLoginXpathNotLinked) throws Throwable {
driverWait = new WebDriverWait(driver, 20);
driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(socialLoginXpathNotLinked)));
driverElement = driver.findElement(By.xpath(socialLoginXpathNotLinked));
if(!driverElement.isDisplayed())
{
isLinked = true;
}
}
#Then("^I unlink user if \"([^\"]*)\" linked$")
public void unlinkUser(String socialLoginXpathLinked) throws Throwable {
if(isLinked)
{
driver.findElement(By.xpath(socialLoginXpathLinked)).click();
isLinked = false;
}
}
And where I call my browser initialization which is what JeffC recommended:
#Given("^I use \"([^\"]*)\" browser$")
public void browserInitialization(String browser) throws Throwable {
if (browser.equals("Chrome"))
{
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browser.equals("Firefox"))
{
System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
driver = new FirefoxDriver();
}
else if(browser.equals("Edge"))
{
System.setProperty("webdriver.edge.driver", "C:\\Program Files (x86)\\Edge Driver\\MicrosoftWebDriver.exe");
driver = new EdgeDriver();
}
else if(browser.equals("IE11"))
{
System.setProperty("webdriver.ie.driver", "C:\\Program Files (x86)\\Internet Explorer\\IEDriverServer32.exe");
driver = new InternetExplorerDriver();
}
}
And on the cucumber side I use a scenario outline I just use a data table for browser grabbing:
|Browser|
|Chrome|
|FireFox|
|Edge|
|IE11|
Related
While using selenium with java, WebdriverManager is not running and the below code is giving null pointer exception. I have returned the driver at end of class.
I have one ask whether should I keep the Webdriver driver as static or not.
import io.github.bonigarcia.wdm.WebDriverManager;
public class Browserselector {
public WebDriver driver;
public static Properties prop;
public WebDriver initializeDriver() throws IOException {
{
String browserName = "firefox";
System.out.println(browserName);
if (browserName.contains("Chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
} else if (browserName.contains("IE")) {
WebDriverManager.iedriver().setup();
driver = new InternetExplorerDriver();
} else if (browserName.contains("FireFox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
} else if (browserName.contains("EDGE")) {
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
}
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("google.com");
return driver;
}
}
Thanks for your help in advance.
you are trying to start "firefox" - but the if condition checks for "Firefox", if you want to use it like that change the following condition
browserName.contains("FireFox")
into
browserName.equalsIgnoreCase("FireFox")
I recommend you to change the nested if with a "switch" it's more readable and easy to follow/understand
Also, don't use a URL without specifying the protocol
driver.get("https://www.google.com");
I am trying to upload the file in selenium using java. When I run my test case then it passed every time but the file is not uploading. See below site and my code where I performing.
Site URL: https://files.fm/
Xpath where i want to upload: //input[#name='file_upload[]']
Note: If this xpath is incorrect then please update in comment.
Code:
#BeforeTest
public void OpenBrowser() {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://files.fm/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void FileUpload() throws InterruptedException, IOException {
Thread.sleep(2000);
WebElement file = driver.findElement(By.xpath("//input[#name='file_upload[]']"));
file.sendKeys("C:/Users/Admin/Pictures/Lighthouse.jpg");
}
use below code :
WebElement file = driver.findElement(By.xpath("//input[#id='file_upload']//following-sibling::input"));
file.sendKeys("C:/Users/Admin/Pictures/Lighthouse.jpg");
WebElement startUploadButton = driver.findElement(By.xpath("//div[#id='savefiles']//div"));
startUploadButton.click();
Hope that helps you:)
You choosed the wrong input. I tried to send to the other input and it worked
Here is xpath of it.
String inPath = "//*[#id='uploadifive-file_upload']/input[2]";
I just have learned about selenium.
My code:
public class FirstTest {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://mail.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("Email")).sendKeys(" YOUR USER NAME");
driver.findElement(By.id("Passwd")).sendKeys("YOUR PASSWORD");
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.id("signIn")).click();
driver.findElement(By.xpath("//div[#class='z0']/div")).click();
driver.findElement(By.xpath("//div[#class='gb_1 gb_3a gb_nc gb_e']/div/a")).click();
driver.findElement(By.xpath("//*[#id='gb_71']")).click();
driver.close();
}
}
Issue is Firefox didnt run. I use Firefox 52.0.1 and Selenium 3.8.1 Anyone can check why for me !
After click run as java application:
I have problem with testing my app which is intended for online shopping. I noticed that all buttons from application working but I have problem with system buttons(look at the picture). I’m using to recognize the button UiAutomatorViewer.
At the picture I’m trying add product to cart but when I’m click button “Dodaj”(means Add) the window closes but there are any actions in application, the product should be added to cart. I tested this application manually and everything work correct.
I tried to operate the button in many ways but still nothing. Does anyone know how to solve it? It’s very important functions to testing for me.
public class Product extends MainPage {
private AndroidDriver<AndroidElement> driver;
#FindBy(id = "com.mec.leroy:id/product_add_to_cart")
private WebElement addToCart;
#FindBy(xpath = "//android.widget.Button[#index='1']") //problem with button
private WebElement buttonAdd;
#FindBy(id = "com.mec.leroy:id/product_name")
private WebElement productName;
public Product(AndroidDriver driver) {
super(driver);
this.driver = driver;
}
public Product addProduct() throws InterruptedException {
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"do koszyka\"));");
addToCart.click();
buttonAdd.click();
Thread.sleep(5000);
return new Product(driver);
}
public boolean productName() throws InterruptedException {
Thread.sleep(2000);
try {
productName.getText();
return true;
} catch (ElementNotVisibleException e) {
return false;
}
}
public class Connector {
public AndroidDriver Settings() throws IOException, InterruptedException {
runAppium();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "33004db396a6c2d1"); // nazwa urządzenia
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
capabilities.setCapability("noRest", "true");
capabilities.setCapability("fullReset", "false");
// capabilities.setCapability("useKeystore", "true");
//uruchomienie aplikacji z poziomu telefonu
capabilities.setCapability("appPackage", "com.mec.leroy");
capabilities.setCapability("appActivity", "com.mec.leroy.main.activity.MainActivity");
//inicjalizacja połączenia z Appium Server
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
return driver;
}
You are using UiAutomatorViewer (that uses old UiAutomator), but in your tests you are providing UiAutomator2: important to know that these frameworks build snapshot xml in a different way, so your locator might be incorrect.
I suggest to try inspect and interact with your button using official appium-desktop:
Install & launch it
Run the server via appium-desktop
Set exact the same capabilities you have in the code, create new session
Now you can inspect your elements and try to click your button from inspector.
When I am trying to open a site through TestNG, a security page is coming and for that I have click a link text "Continue to this website(not recommended)." that i have done through TestNG coding but it is giving java.lang.NullPointerException error.
and this is the code which i am trying......
public class Registration {
WebDriver driver;
WebElement element;
WebElement element2;
WebDriverWait waiter;
#Test(priority = 1)
public void register_With_Cash() throws RowsExceededException, BiffException, WriteException, IOException
{
File file = new File("D:\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver=new InternetExplorerDriver();
driver.get("https://172.25.155.250/loginpage.aspx");
sleep(10000);
waiter.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Continue to this website (not recommended).")));
driver.findElement(By.linkText("Continue to this website (not recommended).")).click();
sleep(50000);
Thanx in advance for any kind of help.
you can use the below javascript to click the Continue to the Website(not recommended) link.
driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()");