I am working on VCM and have created some test script using Selenium and TestNG (not using webdriver).
The problem is that as soon as it logins it automatically logs-out showing the error that the link was not found. If individual run the scripts run perfectly but when running the Suite it always fails showing above mentioned error.
Here's my code for reference:
#BeforeTest
public void setup()
{
selenium = new DefaultSelenium("localhost",4444,"*firefox","http://BaseURL/");
selenium.start();
selenium.windowMaximize();
}
#AfterTest
public void teardown()
{
this.selenium.close();
}
#Test public void testTC_07() throws Exception {
selenium.open("http://BaseWebsite");
selenium.type("name=j_username", "UserId");
selenium.type("name=j_password", "PassWord");
selenium.click("id=login-button");
selenium.waitForPageToLoad("60000");
selenium.click("id=href_Menus");
selenium.waitForPageToLoad("60000");
selenium.click("link=TestContents");
selenium.waitForPageToLoad("60000");
selenium.click("link=Test");
selenium.waitForPageToLoad("60000");
selenium.click("link=Test_title");
selenium.waitForPopUp("Pop Up Window", "60000");
selenium.selectWindow("null");
verifyEquals(selenium.getTitle(), "Test_Title Properties Window");
verifyTrue(selenium.isTextPresent("Title"));
selenium.type("id=textbox", "");
Thread.sleep(10000);
selenium.click("id=doOK");
String title= selenium.getTitle();
if(title!="Window Title"){
selenium.close();
selenium.selectWindow("null");
selenium.click("link=Logout");
selenium.waitForPageToLoad("60000");
}
else{
TC_07.fail(null);
this.selenium.close();
}
}
Related
Here's my code
public static By headers = By.CssSelector("div[class*=hero-component] h1");
public int getHomePageHeaders()
{
try
{
return webDriver.FindElements(headers).Count;
}
catch (Exception e)
{
return 0;
}
}
Other details: My tests are working properly on Chrome, Firefox, Edge and on iPhone12 but it fails on Safari.
I am using Selenium Webdriver (version 4.3.0)
Screenshot of the error:
QuickWatch
I have generated a runnable jar of my project, and running it on other machine through jenkins.
Jar file executing successfully and generating extent report also,
but there is a blank icon on the place of screenshots.
I have searched on google but did not find any relevant solution to solve my problem.
I am using Intellij IDE and opening the extent report by using the option of "Open in Browser".
When there are more than two screenshots attached(like in above case) with the extent report, it is showing blank icon and showing error when opening the image in new tab.
(When opening the extent report by going into the folder where it is actually saved in the system, it is working as expected).
My code:
#BeforeTest
public void setExtent() {
extent = new ExtentReports(System.getProperty("user.dir")+ "/test-output/Login.html", true);
System.out.println("KKKKKKK");
extentTest = extent.startTest("Start the Execution");
extent.addSystemInfo("Host Name", "Live Contract");
extent.addSystemInfo("User Name", "Shivam Goyal");
extent.addSystemInfo("Environment", "Develop");
extentTest.log(LogStatus.INFO, "Test Execution started");
}
public static String getScreenshot(WebDriver driver, String screenshotName) {
String dateName = new SimpleDateFormat("ddMMyyyyhhmmss").format(new Date());
TakesScreenshot ts = (TakesScreenshot) driver;
File src = ts.getScreenshotAs(OutputType.FILE);
String path = System.getProperty("user.dir")+ "/test-output/"+ screenshotName + dateName + ".png";
File destination = new File(path);
try {
FileUtils.copyFile(src, destination);
}
catch(IOException e){
System.out.println("Capture Failed" +e.getMessage());
}
return path;
}
#Test
public void openURL() throws IOException, InterruptedException, HeadlessException, UnsupportedFlavorException {
System.out.println("test openWeb");
extentTest = extent.startTest("LC1_1.1 - Verify the Login Panel of \"Berater\".");
try {
//Accept All cookies
driver.findElement(By.className(property.getProperty("LiveContractCookies"))).click();
//Test case LC1_1.1 Open the given URL in Browser (Aufruf der entsprechenden URL im Browser)
boolean Homescreen = driver.findElement(By.name(property.getProperty("BeraterLoginPanelName1"))).isDisplayed();
Thread.sleep(3000);
Assert.assertTrue(Homescreen);
} catch (NoSuchElementException e) {
e.printStackTrace();
}
}
#Test
public void checkEntryFields() throws InterruptedException {
extentTest = extent.startTest("LC1_1.2 - Verify the Entry fields on Berater's login panel");
try {
driver.findElement(By.className(property.getProperty("LiveContractCookies"))).click();
WebElement username = driver.findElement(By.name(property.getProperty("username_fieldName")));
WebElement password = driver.findElement(By.name(property.getProperty("password_fieldName")));
username.click();
username.sendKeys("Test123");
password.click();
password.sendKeys("9876554");
Thread.sleep(1000);
System.out.println(username.getText());
String usnm = username.getAttribute("value");
String pass = password.getAttribute("value");
System.out.println("Something happening");
Assert.assertEquals("Test123", usnm);
Assert.assertEquals("98765", pass);
}
catch(Exception e){
e.printStackTrace();
}
}
#AfterMethod
public void tearDown(ITestResult result) {
if(result.getStatus() == ITestResult.FAILURE){
extentTest.log(LogStatus.FAIL, "Test Case Failed is" +result.getName());
extentTest.log(LogStatus.FAIL, "Test Case Failed is" +result.getThrowable());
String screenshotPath = Login.getScreenshot(driver, result.getName());
extentTest.log(LogStatus.FAIL, extentTest.addScreenCapture(screenshotPath));
}
else if(result.getStatus() == ITestResult.SKIP){
extentTest.log(LogStatus.SKIP, "Test Case Skipped is "+result.getName());
}
else if(result.getStatus() == ITestResult.SUCCESS){
extentTest.log(LogStatus.PASS, "Test Case Passed is "+result.getName());
}
extent.endTest(extentTest);
//extent.flush();
driver.quit();
//tempDriver.quit();
//tempDriver.quit();
}
#AfterTest
public void endReport() {
try{
extent.flush();
}
catch(Exception e){
e.getMessage();
}
//extent.close();
}
WinAppDriver sometimes find element, sometimes no... I try to log my elements List size, and when I run my test sometimes it return 1, sometimes 0. My code:
public class Test {
public WindowsDriver<RemoteWebElement > driver;
#Before
public void setup() {
try {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("app", "C:\\Program Files\\App\\Bin\\MainFrame.exe");
capabilities.setCapability("platformName", "Windows");
capabilities.setCapability("deviceName", "WindowsPC");
driver = new WindowsDriver<RemoteWebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
}catch(Exception e){
e.printStackTrace();
} finally {
}
}
#Test
public void listLength() {
System.out.println(driver.findElementsByClassName("MaskEdit").size());
}
}
In log I see NoSuchElementException:
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 2.04 seconds
The application MainFrame.exe always runs correctly but test always returns different result.
Why is this happening?
this may happen if the application may not be properly loaded at time , try adding explicit wait.
I try to run the same end2end test parallel (load test, JMeter with JUnitSampler).
If i run parallel tests with real chromeBrowser, its working fine. If i run the Tests with chromeOption “--headless”, no tests are running. What could be the difference between real chromeBrowser and headless chromeBrowser?
One test with headlessBrowser (not parallel) is also running well.
Step1 - JUnit Test:
public class AppTest extends TestCase{
private Logger logger;
private WebDriver driver;
private String user;
private String passwd;
#Before
#Override
public void setUp() throws MalformedURLException {
logger = LoggerFactory.getLogger(AppTest.class);
user = “user”;
passwd = “12345”;
DesiredCapabilities caps = DesiredCapabilities.chrome();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments(“--headless”);
chromeOptions.addArguments(“--incognito”);
caps.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
driver = new RemoteWebDriver(new URL(“http://127.0.0.1:4444/wd/hub”), caps);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
#Test
public void workFlow() throws InterruptedException {
driver.get("https://somesite.de");
try {
WebElement input = driver.findElement(By.id(“login-form-username”));
input.click();
input.clear();
input.sendKeys(user);
} catch (NoSuchElementException e) {
fail(“Element not found”);
}
try {
Thread.sleep(1000);
WebElement myButton = driver.findElement(By.className("my-button”));
myButton.click();
} catch (NoSuchElementException e) {
fail(“Element not found”);
}
try {
Thread.sleep(2000);
WebElement input = driver.findElement(By.id(“password-form-password”));
input.click();
input.sendKeys(passwd);
} catch (NoSuchElementException e) {
fail(“Element not found”);
}
try {
WebElement myButton = driver.findElement(By.className(“my-button”));
myButton.click();
} catch (NoSuchElementException e) {
fail(“Element not found”);
}
try {
Thread.sleep(2000);
List<WebElement> displayBoxHeaders = driver.findElements(By.className(“box-complete-header”));
assertThat(displayBoxHeaders.get(1).getText()).contains(user);
WebElement logo = displayBoxHeaders.get(0);
String svg = logo.findElement(By.tagName(“svg”)).findElement(By.tagName(“g”)).findElement(By.tagName(“path”)).getAttribute(“class”);
assertThat(svg).isNotNull().isNotEmpty();
} catch (NoSuchElementException e) {
fail();
}
try {
WebElement displayText = driver.findElement(By.id(“ui-form-text”));
String text = displayText.getText();
assertThat(text).isNotNull().isNotEmpty();
} catch (Exception e) {
fail();
}
try {
WebElement myButton = driver.findElement(By.className(my-button”));
myButton.click();
} catch (NoSuchElementException e) {
fail(“Element not found”);
}
}
#After
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
Step2 - JUnitSampler in JMeter with JUnitTestClass above.
Step3 - SeleniumGrid in Docker
git clone https://github.com/SeleniumHQ/docker-selenium.git
cd docker-selenium
docker run -d -p 4444:4444 --name selenium-hub selenium/hub:3.5.3-boron
docker run -d --link selenium-hub:hub -e NODE_MAX_INSTANCES=100 -e NODE_MAX_SESSION=100 selenium/node-chrome:3.5.3-boron
Step3 - Run single Thread in JMeter:
Test runs successfully.
Step4 - Run 5 Threads in parallel in JMeter:
First test runs successfully, other four not.
Error: is ElementNotFound in each failed test run
It is important for the test that the browser instances do not have the same session.
Selenium Firefox Browser has no internet connection :- when i try to run my selenium script using firefox , I get no internet connection error where as i do have good internet connection. Can anyone help?Thanks in advance
Here is the code, Its a dataprovider code to be able to create the data driven tests. But everytime i run this whether on firefox or be it chrome its not working
public class Test2 {
//webdriver object
WebDriver driver;
//test annotation
#Test(dataProvider="wordpressdata")
public void loginTest(String Username, String Password) throws InterruptedException{
//Setting the system property to use chrome exe
System.setProperty("webdriver.chrome.driver","C:\\Downloads\\Softwares\\chromedriver_win32\\chromedriver.exe" );
driver=new ChromeDriver();
driver.navigate().to("http://demosite.center/wordpress/wp-login.php");
Thread.sleep(5000);
driver.findElement(By.id("user_login")).sendKeys(Username);
driver.findElement(By.id("user_pass")).sendKeys(Password);
driver.findElement(By.xpath(".//*[#id='wp-submit']")).click();
}
//this is after method for driver quit
#AfterMethod
public void teardown(){
driver.quit();
}
//this is data provider
#DataProvider(name="wordpressdata")
public Object[][] passdata(){
Object[][] data = new Object[3][2];
data[0][0] = "Admin";
data[0][1] = "demo123";
data[1][0] = "Admin";
data[1][1] = "demo23";
data[2][0] = "Admin";
data[2][1] = "demo14";
return data;
}
}
This code is not letting me open it in either Firefox browser or Chrome.