-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddStudent_70.java
60 lines (50 loc) · 2.12 KB
/
AddStudent_70.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package unitn.sectest;
import org.junit.*;
import org.xml.sax.helpers.AttributesImpl;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.InputElementFactory;
import net.sourceforge.jwebunit.api.IElement;
import net.sourceforge.jwebunit.htmlunit.HtmlUnitElementImpl;
import net.sourceforge.jwebunit.junit.*;
public class AddStudent_70 {
private WebTester tester;
@Before
public void prepare(){
tester = new WebTester();
tester.setBaseUrl("http://localhost/schoolmate");
tester.beginAt("/index.php");
tester.setTextField("username", "schoolmate");
tester.setTextField("password", "schoolmate");
tester.submit();
tester.assertTitleEquals("SchoolMate - School Name");
tester.clickLinkWithText("Students");
tester.assertMatch("Manage Students");
}
@Test
public void page(){
tester.setWorkingForm("students");
tester.setTextField("page", "1'> <a href =\"http://unitn.it\">malicious link</a> </ br'");
tester.clickButtonWithText ("Add");
tester.assertMatch("Add New Student");
tester.assertLinkNotPresentWithText("malicious link");
}
@Test
public void page2(){
tester.setWorkingForm("students");
tester.setTextField("page2", "20'> <a href =\"http://unitn.it\">malicious link</a> </ br'");
addSubmitButton("//form[@name='students']");
tester.submit();
tester.assertMatch("Add New Student");
tester.assertLinkNotPresentWithText("malicious link");
}
private void addSubmitButton(String fromXpath){
IElement element = tester.getElementByXPath(fromXpath);
DomElement form= ((HtmlUnitElementImpl)element).getHtmlElement();
InputElementFactory factory = InputElementFactory.instance;
AttributesImpl attributes = new AttributesImpl();
attributes.addAttribute("", "", "type", "", "submit");
HtmlElement submit = factory.createElement(form.getPage(), "input", attributes);
form.appendChild(submit);
}
}