-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixtures.ts
142 lines (125 loc) · 4.71 KB
/
fixtures.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { test as base, expect, BrowserContext } from "@playwright/test";
import {
userAgents,
desktopViewports,
iphoneViewports,
androidViewports,
ipadViewports,
androidTabletViewports,
} from "./utils/useragents"; // Import user agents and viewports
import { getRandomUserAgentAndViewport } from "./utils/useragents";
import { getRandomLocation } from "./utils/geolocation";
type TestFixtures = {
context: BrowserContext;
page: import("@playwright/test").Page;
};
export const test = base.extend<TestFixtures>({
// Override the context fixture to use a random user agent and viewport
context: async ({ browser }, use, testInfo) => {
// Generate a random probability between 0.65 and 0.8 for mobile devices
const mobileProbability = Math.random() * (0.8 - 0.65) + 0.65;
// Choose whether to use mobile or desktop/laptop based on the random probability
const isMobile = Math.random() < mobileProbability;
// Select a random user agent based on the chosen device type
let randomAgent;
if (isMobile) {
randomAgent = userAgents.filter((agent) => agent.deviceType === "mobile")[
Math.floor(
Math.random() *
userAgents.filter((agent) => agent.deviceType === "mobile").length
)
];
} else {
randomAgent = userAgents.filter(
(agent) =>
agent.deviceType === "desktop" || agent.deviceType === "laptop"
)[
Math.floor(
Math.random() *
userAgents.filter(
(agent) =>
agent.deviceType === "desktop" || agent.deviceType === "laptop"
).length
)
];
}
// Log the user agent being used
console.log(
`Test "${testInfo.title}" is using User Agent: ${randomAgent.userAgent} with Mobile Probability: ${mobileProbability}`
);
// Determine the viewport based on the device type
let viewport;
if (
randomAgent.deviceType === "desktop" ||
randomAgent.deviceType === "laptop"
) {
// Randomly select a viewport from the desktop/laptop viewports
viewport =
desktopViewports[Math.floor(Math.random() * desktopViewports.length)];
} else if (randomAgent.deviceType === "mobile") {
// Select mobile viewport based on the device (iPhone or Android)
if (randomAgent.userAgent.includes("iPhone")) {
viewport =
iphoneViewports[Math.floor(Math.random() * iphoneViewports.length)];
} else {
viewport =
androidViewports[Math.floor(Math.random() * androidViewports.length)];
}
} else if (randomAgent.deviceType === "tablet") {
// Further distinguish between iPad and Android tablets
if (randomAgent.tabletType === "ipad") {
viewport =
ipadViewports[Math.floor(Math.random() * ipadViewports.length)];
} else {
viewport =
androidTabletViewports[
Math.floor(Math.random() * androidTabletViewports.length)
];
}
}
// Ensure that a viewport was found, if not, throw an error
if (!viewport) {
throw new Error(
`No viewport found for the user agent: ${randomAgent.userAgent}`
);
}
// Log the selected viewport
console.log(`Using viewport: ${JSON.stringify(viewport)}`);
// Create a new browser context with the selected user agent and viewport
const context = await browser.newContext({
userAgent: randomAgent.userAgent,
viewport,
});
// Use the context in tests
await use(context);
// Clean up the context
await context.close();
},
// Override the page fixture to use the context with the random user agent and viewport
page: async ({ context }, use) => {
const page = await context.newPage();
await use(page);
await page.close();
},
});
export { expect };
// Helper to create a new context with random user agent, viewport, and geolocation
export async function createRandomContext(browser) {
// Get a random user agent and viewport
const { userAgent, viewport } = getRandomUserAgentAndViewport();
// Get a random location for geolocation
const location = getRandomLocation();
// Log the selected random values
console.log(`Selected User Agent: ${userAgent}`);
console.log(`Selected Viewport: ${JSON.stringify(viewport)}`);
console.log(
`Selected Geolocation: ${location.name}, Lat: ${location.lat}, Lon: ${location.lon}`
);
// Create a new browser context with geolocation, random user agent, and random viewport
return await browser.newContext({
geolocation: { latitude: location.lat, longitude: location.lon },
permissions: ["geolocation"], // Ensure geolocation permission is granted
viewport, // Set the random viewport
userAgent, // Set the random user agent
});
}