Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automation tests for gmail, rest api, soap ws #30

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>automation.com</groupId>
<artifactId>qa</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.13</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.42.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.43.1</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>javax.xml.soap</groupId>
<artifactId>javax.xml.soap-api</artifactId>
<version>1.3.5</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package core.tests.com;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class FeatureTests {

// We take url from testng file <parameter name="url"
// value="https://mail.google.com" />
@Test
@Parameters({ "url" })
public void simpleTestLogInToGmail(String url) throws InterruptedException {
// Run fire fox
WebDriver driver = new FirefoxDriver();
driver.get(url);
driver.manage().window().maximize(); // maximize window
Thread.sleep(5000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try implicit wait instead


// Log in to gmail

// Find object
WebElement loginField = getElementById(driver, "Email");
WebElement passwordField = getElementById(driver, "Passwd");
WebElement signInButton = getElementById(driver, "signIn");

// Fill in form and log in
loginField.sendKeys("[email protected]");
passwordField.sendKeys("XXXXXXXX");
signInButton.click();
Thread.sleep(5000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explicit waits for particular elements?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It simple example, usually I use wait methods which scan ui
public static void waitObject(WebDriver driver, String locator)
throws InterruptedException {
for (int i = 0; i < 10; i++) {
Thread.sleep(500);
if (GetObject(driver, locator) != null)
break;
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you're using this for your common code, you should definitely look at explicit wait mechanism

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never heard about it, could you explain me please

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I'll try to use


// delete all emails from inbox
WebElement checkBoxAllEmails = getElementByXpath(
driver,
"//html/body/div[7]/div[3]/div/div[2]/div/div[2]/div/div/div/div/div/div/div/div/div/div/div/div/span/div");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could it be optimized?.. it's not good to put absolute xpath as an example...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I often use css selector or id, it example for beginners how to use xpath. And although I use tree layers, all locators store in Repository

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you want to teach beginners, how to write xpath, you should show a valid example... otherwise, they will start to create incorrect locators looking at this example...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I show how to take xpath from firebug

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use this locator in my tests and it work fine)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if tomorrow e.g. 6th div index will be changed, your test will fail...
if tomorrow e.g. 10th div will be changed to span, your test will fail...
etc. ...
knowledge base should teach how to implement only valid things... otherwise, everyone will doubt in our skills

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you slight change on UI will failed test

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you doubt in my skills, just delete my pull request.
Sorry for wasting your time.

Regards,
Slava

WebElement deleteButton = getElementByXpath(
driver,
"//html/body/div[7]/div[3]/div/div[2]/div/div[2]/div/div/div/div/div/div/div/div/div/div[2]/div[3]/div/div");

checkBoxAllEmails.click();
Thread.sleep(5000);
deleteButton.click();

Thread.sleep(5000);
driver.quit();

}

private WebElement getElementById(WebDriver driver, String id) {

return driver.findElement(By.id(id));
}

private WebElement getElementByXpath(WebDriver driver, String xpath) {

return driver.findElement(By.xpath(xpath));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package core.tests.com;

import java.io.IOException;
import java.net.SocketException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class JsonControler {

@Test
@Parameters({ "url" })
public void simpleTestLogInToGmail(String url) throws InterruptedException,
ClientProtocolException, IOException, JSONException {

// Create json
String json = "{\"user1\": {\"name\": \"Jamse\",\"email\": \"[email protected]\",\"age\": 50,}"
+ ",\"user2\": {\"name\": \"Bond\",\"email\":\"[email protected]\",\"age\": 49,},"
+ " \"user3\": {\"name\": \"Weider\"\"email\": \"[email protected]\",\"age\": 500,}}";

// Send json to api
HttpResponse response = sendJSon(url, json);


//Compare result
Assert.assertTrue(response.toString().contains("200"));
System.out.println("DEBUG1:" + response);


}

public static HttpResponse sendJSon(String url, String json)
throws ClientProtocolException, IOException, JSONException {
HttpPost request = new HttpPost(
"https://api.rest-test-app.com/users/add"); // meyhod post
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it a public API?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it's example. I'm going to create rest api on site for trying

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please provide real URL for test, any existing public
Thanks

StringEntity entity = new StringEntity(json);
entity.setContentType("application/json;charset=UTF-8");
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json;charset=UTF-8"));
request.addHeader("Authorization", "Basic "); // if we need
// authorization
request.setEntity(entity);
HttpResponse response = null;
HttpClient httpclient = HttpClientBuilder.create().build();
try {
response = httpclient.execute(request);
System.out.println(response);
return response;
} catch (SocketException se) {
throw se;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package core.tests.com;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;

import org.testng.Assert;
import org.testng.annotations.Test;

public class WebServiceTest {

@Test
public void testForWS()throws Exception {

SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory
.newInstance();
SOAPConnection soapConnection = soapConnectionFactory
.createConnection();

// Send SOAP Message to SOAP Server
String url = "http://helpqaspb.com:8080/ws/";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it a local server?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, public server on https://ru.godaddy.com


String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" "
+ "xmlns:gs=\"http://spring.io/guides/gs-producing-web-service\"> <soapenv:Header/>"
+ "<soapenv:Body>"
+ "<gs:getCountryRequest>"
+ "<gs:name>Spain</gs:name>"
+ "</gs:getCountryRequest>"
+ "</soapenv:Body>" + "</soapenv:Envelope>";
SOAPMessage soapResponse = soapConnection.call(
getSoapMessageFromString(xml), url);

// Process the SOAP Response
String responce = printSOAPResponse(soapResponse);
//Check responce
Assert.assertTrue(responce.contains("<ns2:currency>EUR</ns2:currency>"));
Assert.assertTrue(responce.contains("<ns2:population>46704314</ns2:population>"));


}

public static SOAPMessage getSoapMessageFromString(String xml)
throws SOAPException, IOException {
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory
.createMessage(
new MimeHeaders(),
new ByteArrayInputStream(xml.getBytes(Charset
.forName("UTF-8"))));
return message;
}

/**
* Method used to print the SOAP Response
*/
private static String printSOAPResponse(SOAPMessage soapResponse)
throws Exception {
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.print("\nResponse SOAP Message = ");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);

StringWriter writer = new StringWriter();
StreamResult result1 = new StreamResult(writer);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer1 = tFactory.newTransformer();
transformer1.transform(sourceContent,result1);
return writer.toString();
}

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1">
<parameter name="url" value="https://mail.google.com" />
<parameter name="dbServer" value="" />
<parameter name="passwordToDataBase" value="" />
<parameter name="dbName" value="" />
<test name="Regression1">
<classes>
<class name="core.tests.com.WebServiceTest"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's about all other tests?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just insert classes which you want to run




</classes>
</test>
</suite>