Skip to content

Commit e9b6d60

Browse files
committed
Updated bean class
1 parent 26df937 commit e9b6d60

File tree

5 files changed

+107
-28
lines changed

5 files changed

+107
-28
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ Thumbs.db
4949

5050
# Other IDE's#
5151
##############
52-
\.classpath
5352
\.project
5453
\.settings
5554
\.dorsync

pom.xml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
<version>${spring.framework.version}</version>
3434
<scope>test</scope>
3535
</dependency>
36+
<dependency>
37+
<groupId>org.springframework</groupId>
38+
<artifactId>spring-test</artifactId>
39+
<version>${spring.framework.version}</version>
40+
<scope>test</scope>
41+
</dependency>
3642
<dependency>
3743
<groupId>org.slf4j</groupId>
3844
<artifactId>slf4j-log4j12</artifactId>
@@ -44,12 +50,6 @@
4450
<artifactId>slf4j-api</artifactId>
4551
<version>1.7.7</version>
4652
</dependency>
47-
<dependency>
48-
<groupId>org.springframework</groupId>
49-
<artifactId>spring-test</artifactId>
50-
<version>${spring.framework.version}</version>
51-
<scope>test</scope>
52-
</dependency>
5353
<dependency>
5454
<groupId>javax.inject</groupId>
5555
<artifactId>javax.inject</artifactId>
@@ -97,6 +97,4 @@
9797
</build>
9898
</profile>
9999
</profiles>
100-
101-
102100
</project>

src/test/java/com/techboy/selenium/beanconfig/Beans.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,41 @@
44
import com.techboy.selenium.config.BrowserCapabilities;
55
import org.openqa.selenium.Proxy;
66
import org.springframework.context.annotation.*;
7+
import org.springframework.core.env.Environment;
78
import org.springframework.core.type.AnnotatedTypeMetadata;
89

910
import javax.annotation.PostConstruct;
10-
import java.io.File;
11+
import javax.inject.Inject;
1112
import java.io.IOException;
1213

1314
import static org.openqa.selenium.Proxy.ProxyType.MANUAL;
1415

1516
/**
1617
* Created by christopher on 01/12/2015.
1718
*/
19+
@PropertySource("classpath:app.properties")
1820
@Configuration
19-
public class Beans {
21+
public class Beans {
22+
23+
@Inject
24+
Environment env;
2025

2126
private String workingOS = System.getProperty("os.name").toLowerCase();
2227
private final boolean proxyEnabled = Boolean.getBoolean("proxyEnabled");
2328
private final String proxyHostname = System.getProperty("proxyHost");
2429
private final Integer proxyPort = Integer.getInteger("proxyPort");
2530
private final String proxyDetails = String.format("%s:%d", proxyHostname, proxyPort);
26-
private static String browser = System.getProperty("browser", "chrome");
31+
private static String browser="firefox";
2732

2833

2934
@PostConstruct
3035
public void systemPath() throws IOException {
3136
if (workingOS.contains("windows")) {
32-
System.setProperty("webdriver.chrome.driver", new File(".").getCanonicalPath()+"/src/test/resources/selenium_browser_drivers/windowsChromedriver/chromedriver.exe");
37+
System.setProperty("webdriver.chrome.driver","selenium_browser_drivers/windowsChromedriver/chromedriver.exe");
3338
} else if (workingOS.contains("mac")) {
34-
System.setProperty("webdriver.chrome.driver", new File(".").getCanonicalPath()+"/src/test/resources/selenium_browser_drivers/macChromedriver/chromedriver");
39+
System.setProperty("webdriver.chrome.driver", "selenium_browser_drivers/macChromedriver/chromedriver");
3540
} else if (workingOS.contains("linux")) {
36-
System.setProperty("webdriver.chrome.driver", new File(".").getCanonicalPath()+"/src/test/resources/selenium_browser_drivers/linuxChromedriver/chromedriver");
41+
System.setProperty("webdriver.chrome.driver", "selenium_browser_drivers/linuxChromedriver/chromedriver");
3742
}
3843

3944
}
@@ -50,42 +55,50 @@ public Proxy proxy() {
5055
return new Proxy();
5156
}
5257

53-
@Bean
54-
@Conditional(FirefoxCondition.class)
58+
@Bean(destroyMethod="quit")
59+
@Conditional(Beans.FirefoxCondition.class)
5560
public BrowserDriverExtended.FirefoxDriverExtended firefox() {
5661
return new BrowserDriverExtended.FirefoxDriverExtended(BrowserCapabilities.newInstance().getFirefoxCapabilities());
5762
}
5863

5964

60-
@Bean
61-
@Conditional(ChromeCondition.class)
65+
@Bean(destroyMethod="quit")
66+
@Conditional(Beans.ChromeCondition.class)
6267
public BrowserDriverExtended.ChromeDriverExtended chrome() {
6368
return new BrowserDriverExtended.ChromeDriverExtended(BrowserCapabilities.newInstance().getChromeCapabilities());
6469
}
6570

66-
67-
public static class FirefoxCondition implements Condition {
71+
private static class FirefoxCondition implements Condition {
6872
@Override
6973
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
7074
return browser.contentEquals("firefox") || browser.contentEquals("");
7175
}
7276

7377
}
7478

75-
public static class ChromeCondition implements Condition {
79+
private static class ChromeCondition implements Condition {
7680
@Override
7781
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
7882
return browser.contentEquals("chrome");
7983
}
8084

8185
}
8286

83-
public static class IECondition implements Condition {
87+
private static class IECondition implements Condition {
8488
@Override
8589
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
8690
return browser.contentEquals("IE");
8791
}
88-
8992
}
9093

94+
95+
96+
97+
9198
}
99+
100+
101+
102+
103+
104+

src/test/java/test/TestGoogle.java

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import com.techboy.selenium.beanconfig.Beans;
44
import org.junit.Test;
55
import org.junit.runner.RunWith;
6+
import org.openqa.selenium.By;
67
import org.openqa.selenium.WebDriver;
8+
import org.openqa.selenium.WebElement;
9+
import org.openqa.selenium.support.ui.ExpectedCondition;
10+
import org.openqa.selenium.support.ui.WebDriverWait;
711
import org.springframework.beans.factory.annotation.Autowired;
812
import org.springframework.test.context.ContextConfiguration;
913
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -19,13 +23,77 @@ public class TestGoogle {
1923

2024

2125
@Autowired
22-
private WebDriver browser;
26+
private WebDriver driver;
2327

2428
@Test
25-
public void lunchBrowser() throws InterruptedException {
26-
browser.get("http://www.google.com");
27-
Thread.sleep(1000l);
29+
public void googleCheeseExample() throws Exception {
30+
// Create a new WebDriver instance
31+
// Notice that the remainder of the code relies on the interface,
32+
// not the implementation.
33+
// And now use this to visit Google
34+
driver.get("http://www.google.com");
35+
// Alternatively the same thing can be done like this
36+
// driver.navigate().to("http://www.google.com");
37+
38+
// Find the text input element by its name
39+
WebElement element = driver.findElement(By.name("q"));
40+
41+
// Enter something to search for
42+
element.clear();
43+
element.sendKeys("Cheese!");
44+
45+
// Now submit the form. WebDriver will find the form for us from the element
46+
element.submit();
47+
48+
// Check the title of the page
49+
System.out.println("Page title is: " + driver.getTitle());
50+
51+
// Google's search is rendered dynamically with JavaScript.
52+
// Wait for the page to load, timeout after 10 seconds
53+
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
54+
public Boolean apply(WebDriver d) {
55+
return d.getTitle().toLowerCase().startsWith("cheese!");
56+
}
57+
});
58+
59+
// Should see: "cheese! - Google Search"
60+
System.out.println("Page title is: " + driver.getTitle());
2861
}
2962

63+
@Test
64+
public void googleMilkExample() throws Exception {
65+
// Create a new WebDriver instance
66+
// Notice that the remainder of the code relies on the interface,
67+
// not the implementation.
68+
3069

70+
// And now use this to visit Google
71+
driver.get("http://www.google.com");
72+
// Alternatively the same thing can be done like this
73+
// driver.navigate().to("http://www.google.com");
74+
75+
// Find the text input element by its name
76+
WebElement element = driver.findElement(By.name("q"));
77+
78+
// Enter something to search for
79+
element.clear();
80+
element.sendKeys("Milk!");
81+
82+
// Now submit the form. WebDriver will find the form for us from the element
83+
element.submit();
84+
85+
// Check the title of the page
86+
System.out.println("Page title is: " + driver.getTitle());
87+
88+
// Google's search is rendered dynamically with JavaScript.
89+
// Wait for the page to load, timeout after 10 seconds
90+
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
91+
public Boolean apply(WebDriver d) {
92+
return d.getTitle().toLowerCase().startsWith("milk!");
93+
}
94+
});
95+
96+
// Should see: "cheese! - Google Search"
97+
System.out.println("Page title is: " + driver.getTitle());
98+
}
3199
}

src/test/resources/app.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
browser= firefox

0 commit comments

Comments
 (0)